Android: how to make a view grow to fill available space?

前端 未结 4 1789
清酒与你
清酒与你 2021-01-30 19:27

This seems straightforward, but I can\'t figure out how to do it. I have a horizontal layout with an EditText and two ImageButtons. I want the ImageButtons to be of a fixed si

相关标签:
4条回答
  • 2021-01-30 19:54

    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.

    0 讨论(0)
  • 2021-01-30 19:59

    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>
    
    0 讨论(0)
  • 2021-01-30 20:03

    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!

    0 讨论(0)
  • 2021-01-30 20:10

    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.

    0 讨论(0)
提交回复
热议问题