Android 設定子控制項的寬度或高度為 match_parent來填充父控制項中的剩餘寬度或高度的方法

來源:互聯網
上載者:User

標籤:

先上幾張, 如下:

    

 

 

上述四張圖要實現的布局效果是: 假如父控制項中包含兩個子控制項, 其中一個子控制項(中為紅色button)的寬度是固定數值, 而另一個子控制項(中為綠色button)的寬度不固定, 要想讓這兩個子控制項的總寬度剛好等於父控制項的寬度.可以將寬度不固定的那個控制項的寬度設定為match_parent來實現, 但有些細節需要注意, 否則即使設定了match_parent, 也不能出現如上的效果. 注意細節如下:

  1. 上述效果可以使用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將會產生重疊.
  2. 上述效果如果使用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來填充父控制項中的剩餘寬度或高度的方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.