ChatGPT解决这个技术问题 Extra ChatGPT

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>

l
linquize

如果您想保持布局相同(即文本后面的按钮),请使用 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 '权重' 使其最后被计算出来并优先于按钮。玩不同的 layout_weights,看看你能有多少乐趣!


谢谢你。我在我的布局 3 个区域(页眉、内容 - WebView 和页脚)中通过设置 WebView layout_weight = 1 和 footer's layout_weight = 0 解决了这个问题。
不知道将权重和 0dp 设置为只有一个孩子的技巧。谢谢!
这通常被认为是最佳做法吗?有趣的是,您可以影响 android 解析视图的顺序。
我认为宽度应该保持为0
F
Federico Ponzi

在相对布局中试试这个

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

谢谢你。这很好用。 (除了 ImageButtons 必须在 EditText 之前定义,正如 Joseph 所指出的)。
fill_parent - 从 API 级别 8 开始不推荐使用此常量,并由 {@code match_parent} 替换。
J
Joseph Earl

好的:

<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计算事物的高度时,它只考虑剩余空间,而不是它可能占用的所有空间如果没有别的东西

EditText 的高度为 fill_parent(2.2+ 中为 match_parent),因此它占用了剩余的可用空间

抱歉,没有阅读您的问题。如果您想以垂直方式进行操作,上面的代码提供了答案。对于水平布局,@Sunil 发布的代码应该可以工作。


谢谢约瑟夫。这确实有效。不过,我将功劳归功于 Sunil 稍微快了一点。
t
taynguyen

使用将 layout_widthlayout_height 设置为 0dp(按您要填充剩余空间的方向)。然后使用 layout_weight 使其填充剩余空间。


J
Jose

我在填充 LinearLayout 时遇到了类似的问题(我正在使用 Artic Fox 版本)。在我的设计中,我在具有垂直方向的 LinearLayout 中有 2 个按钮。就我而言,我通过放置以下内容来解决问题:

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