【问题标题】:Make an element fill all the free space in wrap_content-based layout使元素填充基于 wrap_content 的布局中的所有可用空间
【发布时间】:2013-07-12 17:07:56
【问题描述】:

我有一个垂直布局:2 个文本字段和一个水平按钮栏:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#ff00ff00"
    android:orientation="vertical"
    android:padding="4dp" >

        <EditText
            android:id="@+id/et_email"
            android:layout_width="fill_parent"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:layout_marginTop="2dp"
            android:background="#ff00ffff"
            android:inputType="textEmailAddress" >
        </EditText>

        <EditText
            android:id="@+id/et_comment"
            android:layout_width="fill_parent"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:layout_marginTop="2dp"
            android:background="#ff00ffff"
            android:inputType="textMultiLine"
            android:maxLines="5"
            android:minLines="5" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btn_send"
            android:layout_weight="0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="OK" />

        <Button
            android:id="@+id/btn_show"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Some very long text" />

        <Button
            android:id="@+id/btn_cancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Cancel" />
    </LinearLayout>

</LinearLayout>

我希望按钮栏有wrap_content 大小策略(甚至不能让它们固定大小,因为这个应用程序有不同的本地化)。我还希望文本字段与按钮框具有相同的宽度。以下是选择平台 17 的设计器(Eclipse + ADT)中上述 xml 的外观(以及我希望它的外观)。颜色只是为了方便调试:

这是它在 Android 4.1 平板电脑上的外观:

此布局用作对话框的自定义布局。除了设置内容和标题之外,没有对 Dialog 进行任何操作。

有趣的事实:它在 Android 3.1 平板电脑上看起来与我想要的完全一样(并且与 ADT 设计师展示的一样)。但不是在 4.1 上。

我怎样才能达到想要的布局?

【问题讨论】:

  • 我不明白你的问题(或者它很简单,只需在你的中间按钮上放一个 layout_weight=1 )
  • 很确定他希望线性布局宽度缩小到 3 个按钮的宽度(就像在顶部图片中一样)
  • @njzk2:按钮有wrap_content。对话框本身和两个文本字段应与带有按钮的布局一样宽。正如您在最后一张图片中看到的那样,情况并非如此。
  • 一目了然,一切都在您的 xml 中。我会尝试一些建议,如果要使用 wrap_content 作为高度,请从 EditTexts 中删除权重,否则将高度设置为 0dp,它将使用权重。另外,出于好奇,如果您将按钮移到顶部,EditTexts 上方,它的大小是否合适?
  • 您尝试过我发布的解决方案吗?据我所知,它完全符合您的要求。

标签: android android-layout user-interface


【解决方案1】:

您可能希望使用 RelativeLayout 来完成您正在寻找的内容。尝试将您的 xml 更改为以下内容:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ff00ff00"
        android:padding="4dp" >

        <EditText
            android:id="@+id/et_email"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/buttonbar"
            android:layout_alignRight="@+id/buttonbar"
            android:layout_marginTop="2dp"
            android:background="#ff00ffff"
            android:inputType="textEmailAddress" >
        </EditText>

        <EditText
            android:id="@+id/et_comment"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/buttonbar"
            android:layout_alignRight="@+id/buttonbar"
            android:layout_below="@+id/et_email"
            android:layout_marginTop="2dp"
            android:background="#ff00ffff"
            android:inputType="textMultiLine"
            android:maxLines="5"
            android:minLines="5" />

        <RelativeLayout
            android:id="@+id/buttonbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/et_comment" >

            <Button
                android:id="@+id/btn_send"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@+id/btn_show"
                android:layout_alignTop="@+id/btn_show"
                android:layout_toLeftOf="@+id/btn_show"
                android:text="OK" />

            <Button
                android:id="@+id/btn_show"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:text="Some very long text" />

            <Button
                android:id="@+id/btn_cancel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@+id/btn_show"
                android:layout_alignTop="@+id/btn_show"
                android:layout_toRightOf="@+id/btn_show"
                android:text="Cancel" />

        </RelativeLayout>

    </RelativeLayout>

【讨论】:

  • EditTexts 在你的 xml 中有 wrap_content,这与我需要的相反。
  • 布局参数是 wrap_content 但因为它们被设置为与按钮栏边缘对齐,所以它们将匹配它的大小。
  • 谢谢,有进展:这在 Android 3 和 4 上呈现相同的方式,这是可以接受的。但这不是我想要的样子:i.piccy.info/i7/d09e06c0c597f0b761e0064b5b679bd0/4-63-440/…
  • 其实这个解决方案还有一个问题:因为布局有match_parent而不是wrap_content,所以他们假设Dialog想要的宽度,这对于按钮来说是不够的。左右按钮上的文本都被剪裁了(实际上比我在问题中指定的要长)。
  • 我已经详细说明了你的设计,全部完成了wrap_content,现在它可以工作了。谢谢你的想法。
【解决方案2】:

这看起来很愚蠢,但似乎有效。尝试放入嵌套布局。更改您的外部布局宽度/高度以填充父级。然后,在其中使用您的所有视图等进行另一个线性布局。为内部 LinearLayout 提供 wrap_content 值,这样就可以了。一定有比这更好的方法,但我现在想不出。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多