Android:如何让视图增长以填充可用空间?
这似乎很简单,但我不知道如何去做。 我有一个EditText和两个ImageButtons的水平布局。 我想ImageButtons是一个固定的大小,EditText占据布局中的剩余空间。 这怎么能做到呢?
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content"> </EditText> <ImageButton android:src="@drawable/img1" android:layout_width="50dip" android:layout_height="50dip"> </ImageButton> <ImageButton android:src="@drawable/img2" android:layout_width="50dip" android:layout_height="50dip"> </ImageButton> </LinearLayout> 尝试在相对布局
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content"> <ImageButton android:id="@+id/btn1" android:src="@drawable/icon" android:layout_width="50dip" android:layout_height="50dip" android:layout_alignParentRight="true"/> <ImageButton android:id="@+id/btn2" android:src="@drawable/icon" android:layout_width="50dip" android:layout_height="50dip" android:layout_toLeftOf="@+id/btn1"/> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_toLeftOf="@+id/btn2"> </EditText> </RelativeLayout> 
 如果要保持布局相同(即文本之后的button),请使用layout_weight属性以及fill_parent ,从而: 
 <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <EditText android:layout_weight="1" android:layout_width="fill_parent" android:layout_height="wrap_content"> </EditText> <ImageButton android:src="@drawable/img1" android:layout_width="50dip" android:layout_height="50dip"> </ImageButton> <ImageButton android:src="@drawable/img2" android:layout_width="50dip" android:layout_height="50dip"> </ImageButton> </LinearLayout> 
 给EditText “权重”使它最后得出,并给予button优先。 玩不同的layout_weights,看看你有多less乐趣! 
好的:
 <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content"> <ImageButton android:id="@+id/button1" android:src="@drawable/img1" android:layout_width="50dip" android:layout_alignParentTop="true" android:layout_height="50dip"> </ImageButton> <ImageButton android:src="@drawable/img2" android:layout_width="50dip" android:layout_alignParentTop="true" android:layout_toRightOf="@id/button1" android:layout_height="50dip"> </ImageButton> <EditText android:layout_below="@id/button" android:layout_width="wrap_content" android:layout_height="fill_parent"> </EditText> </RelativeLayout> 
布局的要点是:
-  大小固定的项目首先放置在布局中,这样当Android在文件中稍后用fill_parent来计算事物的高度时,fill_parent考虑剩余空间,而不是所有可能占用的空间如果没有其他的东西在那里
-   EditText有一个fill_parent的高度(match_parent在2.2+),以便占用剩下的可用空间
对不起没读过你的问题就够了。 上面的代码提供了答案,如果你想以垂直方式来做。 对于水平布局,@Sunil发布的代码应该可以工作。
 使用layout_width或layout_height设置为0dp (按您想要填充剩余空间的方向)。 然后使用layout_weight使其填充剩余的空间。