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

ab1*_*b11 84 android android-layout

这似乎很简单,但我无法弄清楚如何做到这一点.我有一个带有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>
Run Code Online (Sandbox Code Playgroud)

Jon*_*han 134

如果要保持布局相同(即文本后面的按钮),请使用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>
Run Code Online (Sandbox Code Playgroud)

赋予EditText'权重'使得它最后得出结论并优先于按钮.玩不同的layout_weights,看看你有多大的乐趣!

  • 宽度应保持0我认为 (2认同)

Sun*_*dey 49

在相对布局中尝试这个

<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>
Run Code Online (Sandbox Code Playgroud)

  • 谢谢.这非常有效.(除了必须在EditText之前定义ImageButtons,正如Joseph所指出的那样). (4认同)

Jos*_*arl 19

好的:

<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>
Run Code Online (Sandbox Code Playgroud)

布局的关键点是:

  • 具有固定大小的项目首先放置在布局中,这样当Android计算事物高度的高度时,fill_parent文件中的后期仅考虑剩余空间,而不是所有可能占用的空间.没有别的东西在那里
  • EditText的高度为fill_parent(match_parent2.2+),因此它占用了剩余的可用空间

抱歉没看过你的问题就够了.如果您想以垂直方式执行此操作,上面的代码将提供答案.对于水平布局,@ Sunun发布的代码应该有效.


tay*_*yen 10

使用设置layout_widthlayout_height0dp(按要填充剩余空间的方向).然后使用layout_weight它来填充剩余空间.