[英]Align and wrap imagebuttons in linearlayout
我有一個linearlayout
,其imagebuttons
在父布局的底部水平對齊。 這正是我想要的樣子:
這就是我的做法:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:background="@color/dim_foreground_disabled_material_dark"
android:orientation="horizontal">
<ImageButton
android:id="@+id/new_1"
android:background="@drawable/_dashboard_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageButton
android:id="@+id/new_2"
android:background="@drawable/_dashboard_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageButton
android:id="@+id/new_3"
android:background="@drawable/_dashboard_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
我還嘗試過將weightsum
分配給linearlayout
,並將weights
分配給imagebuttons
。 但是它擴展了可繪制對象的縱橫比。
我應該如何使用布局將它們布置在視圖的底部,等距分布,居中放置,並且沒有可拉伸對象的任何長寬比。
您可以通過以下兩種方式進行嘗試
1)layout_weight和weightSum
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:background="@color/dim_foreground_disabled_material_dark"
android:orientation="horizontal"
android:weightSum="1">
<Button
android:id="@+id/new_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.33"
android:text="A" />
<Button
android:id="@+id/new_2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.33"
android:text="B" />
<Button
android:id="@+id/new_3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.33"
android:text="C" />
</LinearLayout>
2)更改為“ RelativeLayout”
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:background="@color/dim_foreground_disabled_material_dark"
android:orientation="horizontal" >
<Button
android:id="@+id/new_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="A" />
<Button
android:id="@+id/new_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="B" />
<Button
android:id="@+id/new_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="C" />
</RelativeLayout>
您必須像這樣使用RelativeLayout。
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/dim_foreground_disabled_material_dark">
<ImageButton
android:id="@+id/new_1"
android:background="@drawable/_dashboard_1"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageButton
android:id="@+id/new_2"
android:background="@drawable/_dashboard_2"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageButton
android:id="@+id/new_3"
android:background="@drawable/_dashboard_3"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
如果要使用LinearLayout實現這一目標,請嘗試這一目標。
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="10">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton10"
android:src="@mipmap/ic_launcher"
android:layout_marginLeft="20dip"
android:background="@android:color/transparent"/>
<View android:layout_width="0dip"
android:layout_weight="5"
android:layout_height="1dip"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton9"
android:src="@mipmap/ic_launcher"
android:background="@android:color/transparent"/>
<View android:layout_width="0dip"
android:layout_weight="5"
android:layout_height="1dip"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton8"
android:src="@mipmap/ic_launcher"
android:layout_marginRight="20dip"
android:background="@android:color/transparent"/>
</LinearLayout>
或者,如果您對relativelayout沒問題,請嘗試此...
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:id="@+id/imageButton12"
android:src="@mipmap/ic_launcher"
android:background="@android:color/transparent"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton11"
android:layout_centerInParent="true"
android:src="@mipmap/ic_launcher"
android:background="@android:color/transparent"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton13"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@mipmap/ic_launcher"
android:background="@android:color/transparent"/>
</RelativeLayout>
嘗試-
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:weightSum="3"
android:background="@color/dim_foreground_disabled_material_dark"
android:orientation="horizontal">
<ImageButton
android:id="@+id/new_1"
android:src="@drawable/_dashboard_1"
android:adjustViewBounds="true"
android:scaleType="centerInside"
android:background="@null"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_centerInParent="true" />
<ImageButton
android:id="@+id/new_2"
android:src="@drawable/_dashboard_2"
android:adjustViewBounds="true"
android:scaleType="centerInside"
android:background="@null"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_centerInParent="true" />
<ImageButton
android:id="@+id/new_3"
android:src="@drawable/_dashboard_3"
android:adjustViewBounds="true"
android:scaleType="centerInside"
android:background="@null"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_centerInParent="true" />
</LinearLayout>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.