Android:如何让视图增长来填充可用空间?

时间:2023-02-09 16:46:35

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 size, and the EditText to take up the remaining space in the layout. How can this be accomplished?

这看起来很简单,但是我不知道怎么做。我有一个带有EditText和两个ImageButtons的水平布局。我希望imagebutton的大小是固定的,而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>

4 个解决方案

#1


43  

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>

#2


113  

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

如果希望保持布局相同(例如,文本后面的按钮),使用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>

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!

赋予EditText“权重”使它能够最后被计算出来并优先于按钮。玩不同的layout_weights,看看你能有多少乐趣!

#3


19  

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
  • 项目与固定大小的布局中排名第一,这种方式在Android计算高度的高度用宽和的东西后在文件中只考虑剩余空间,并不是所有的空间可能占领别的不说
  • The EditText has a height of fill_parent (match_parent in 2.2+) so that it takes up the rest of the available space
  • EditText具有fill_parent的高度(在2.2+中为match_parent),因此它将占用其余可用空间

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.

对不起,你的问题没读够。如果您希望以垂直方式进行操作,上面的代码提供了答案。对于水平布局,@Sunil发布的代码应该可以工作。

#4


6  

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.

使用设置layout_width或layout_height到0dp(按您希望填充剩余空间的方向)。然后使用layout_weight来填充剩余空间。

#1


43  

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>

#2


113  

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

如果希望保持布局相同(例如,文本后面的按钮),使用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>

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!

赋予EditText“权重”使它能够最后被计算出来并优先于按钮。玩不同的layout_weights,看看你能有多少乐趣!

#3


19  

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
  • 项目与固定大小的布局中排名第一,这种方式在Android计算高度的高度用宽和的东西后在文件中只考虑剩余空间,并不是所有的空间可能占领别的不说
  • The EditText has a height of fill_parent (match_parent in 2.2+) so that it takes up the rest of the available space
  • EditText具有fill_parent的高度(在2.2+中为match_parent),因此它将占用其余可用空间

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.

对不起,你的问题没读够。如果您希望以垂直方式进行操作,上面的代码提供了答案。对于水平布局,@Sunil发布的代码应该可以工作。

#4


6  

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.

使用设置layout_width或layout_height到0dp(按您希望填充剩余空间的方向)。然后使用layout_weight来填充剩余空间。