Android: 如何使视图增长以填充可用空间?

这看起来很简单,但我不知道怎么做。我有一个带有一个 EditText 和两个 ImageButton 的水平布局。我希望 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>
89261 次浏览

try this in relative layout

<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>

Okay:

<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>

The key points of the layout are:

  • The items with fixed sized are placed first in the layout, this way when Android comes to calculating the height of the height of things with fill_parent later in the file it only takes into account the remaining space, not all of the space it could possibly occupy if nothing else was there
  • The EditText has a height of fill_parent (match_parent in 2.2+) so that it takes up the rest of the available space

Sorry didn't read your question will enough. The above code provides the answer if you want to do it in a vertical fashion. For a horizontal layout the code posted by @Sunil should work.

If you want to keep the layout the same (ie, buttons after the text), use the layout_weight property, along with fill_parent, thus:

<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>

Giving the EditText 'weight' makes it get figured out last and gives precedence to the buttons. Play with the different layout_weights and see how much fun you can have!

Use set the layout_width or layout_height to 0dp (By the orientation you want to fill remaining space). Then use the layout_weight to make it fill remaining space.

I have a similar problem filling a LinearLayout (I´m working with Artic Fox version). In my design, I have 2 buttons in a LinearLayout with vertical orientation. In my case I solve the problem by putting:

android:insetBottom="0dp"
android:insetTop="0dp"