標籤:
先上幾張, 如下:
上述四張圖要實現的布局效果是: 假如父控制項中包含兩個子控制項, 其中一個子控制項(中為紅色button)的寬度是固定數值, 而另一個子控制項(中為綠色button)的寬度不固定, 要想讓這兩個子控制項的總寬度剛好等於父控制項的寬度.可以將寬度不固定的那個控制項的寬度設定為match_parent來實現, 但有些細節需要注意, 否則即使設定了match_parent, 也不能出現如上的效果. 注意細節如下:
- 上述效果可以使用RelativeLayout實現. 但注意綠色button的水平位置必須相對於紅色button來設定, 而不能相對於父控制項來設定. 即: 綠色button只能設定為android:layout_toLeftOf="@id/red_button" 或 android:layout_toRightOf="@id/red_button", 而不能設定為 android:layout_alignParentLeft="true" 或 android:layout_alignParentRight="true". 否則兩個button將會產生重疊.
- 上述效果如果使用LinearLayout實現, 將有一定局限性. 只有當寬度確定的控制項都位於左邊(或上邊), 寬度不確定的控制項位於右邊(或下邊)時, 才能使用LinearLayout. 也就是說, 使用LinearLayout只能實現前兩個圖的效果, 而不能實現後兩個圖的效果(如果用於後兩個效果, 那麼左邊的button將佔據整個螢幕的寬度, 而右邊的button將會被擠到螢幕外了).
附上布局代碼:
1. 圖1的布局:
(1) 使用RelativeLayout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/red_button" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:background="#ff0000" android:text="1" /> <Button android:id="@+id/green_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_toRightOf="@id/red_button" android:background="#00ff00" android:text="222222" /></RelativeLayout>
(2) 使用LinearLayout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <Button android:id="@+id/red_button" android:layout_width="200dp" android:layout_height="wrap_content" android:background="#ff0000" android:text="1" /> <Button android:id="@+id/green_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#00ff00" android:text="222222" /></LinearLayout>
2. 圖2的布局:
(1) 使用RelativeLayout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/red_button" android:layout_width="50dp" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:background="#ff0000" android:text="1" /> <Button android:id="@+id/green_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_toRightOf="@id/red_button" android:background="#00ff00" android:text="222222" /></RelativeLayout>
(2) 使用LinearLayout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <Button android:id="@+id/red_button" android:layout_width="50dp" android:layout_height="wrap_content" android:background="#ff0000" android:text="1" /> <Button android:id="@+id/green_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#00ff00" android:text="222222" /></LinearLayout>
3. 圖3的布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/red_button" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:background="#ff0000" android:text="1" /> <Button android:id="@+id/green_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_toLeftOf="@id/red_button" android:background="#00ff00" android:text="222222" /></RelativeLayout>
4. 圖4的布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/red_button" android:layout_width="50dp" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:background="#ff0000" android:text="1" /> <Button android:id="@+id/green_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_toLeftOf="@id/red_button" android:background="#00ff00" android:text="222222" /></RelativeLayout>
Android 設定子控制項的寬度或高度為 match_parent來填充父控制項中的剩餘寬度或高度的方法