【问题标题】:Horizontal LinearLayouts in a Vertical LinearLayout垂直线性布局中的水平线性布局
【发布时间】:2015-07-27 01:55:08
【问题描述】:

我想要一些右侧带有按钮的 EditText。现在我只是让 Edittexts 用 0dp 技巧填充父级。如果我添加按钮,EditText 的高度将随着 wrap_content 缩小。如果我做 match_parent 一个 EditText 填满整个屏幕。

RelativeLayout 不起作用,因为您无法像 LinearLayout 那样使其匹配父级。

我想获得屏幕高度,然后将布局高度设置为 1/7(因为我有 7 个 EditText)。合理吗?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <EditText android:id="@+id/box_0"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="@string/edit_task"
        android:saveEnabled="true"
        android:inputType="textCapSentences"
        android:maxLength="32"
        android:padding="10dp"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_send"
        android:layout_gravity="end"/>
    <!--red-->
    </LinearLayout>
<EditText android:id="@+id/box_1"
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:saveEnabled="true"
    android:inputType="textCapSentences"
    android:maxLength="32"
    android:padding="10dp"/>
<EditText android:id="@+id/box_2"
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:saveEnabled="true"
    android:inputType="textCapSentences"
    android:maxLength="32"
    android:padding="10dp"/>
<EditText android:id="@+id/box_3"
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:saveEnabled="true"
    android:inputType="textCapSentences"
    android:maxLength="32"
    android:padding="10dp"/>
<EditText android:id="@+id/box_4"
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:saveEnabled="true"
    android:inputType="textCapSentences"
    android:maxLength="32"
    android:padding="10dp"/>
<EditText android:id="@+id/box_5"
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:saveEnabled="true"
    android:inputType="textCapSentences"
    android:maxLength="32"
    android:padding="10dp"/>
<EditText android:id="@+id/box_6"
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:saveEnabled="true"
    android:inputType="textCapSentences"
    android:maxLength="32"
    android:padding="10dp"/>
</LinearLayout>

我只在第一个 EditText 中做了双 LinearLayout。

http://i.imgur.com/t4hN6nO.jpg

看看第二个比其他的要大来补偿,而第一个更小?

【问题讨论】:

  • 你想要哪种布局?

标签: android android-layout android-edittext android-linearlayout android-button


【解决方案1】:

您为什么不在其中添加ScrollView? 将有大量设备永远无法正确显示屏幕中的 7 editText。

无论如何,不​​推荐使用带有重量的嵌套LinearLayout。 但是你可以通过这种方式实现你想做的事情。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:orientation="horizontal">

    <EditText
        android:id="@+id/box_0"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:hint="@string/edit_task"
        android:inputType="textCapSentences"
        android:maxLength="32"
        android:padding="10dp"
        android:saveEnabled="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end"
        android:text="@string/button_send" />
    <!--red-->
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:orientation="horizontal">

    <EditText
        android:id="@+id/box_1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:hint="@string/edit_task"
        android:inputType="textCapSentences"
        android:maxLength="32"
        android:padding="10dp"
        android:saveEnabled="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end"
        android:text="@string/button_send" />
    <!--red-->
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:orientation="horizontal">

    <EditText
        android:id="@+id/box_2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:hint="@string/edit_task"
        android:inputType="textCapSentences"
        android:maxLength="32"
        android:padding="10dp"
        android:saveEnabled="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end"
        android:text="@string/button_send" />
    <!--red-->
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:orientation="horizontal">

    <EditText
        android:id="@+id/box_3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:hint="@string/edit_task"
        android:inputType="textCapSentences"
        android:maxLength="32"
        android:padding="10dp"
        android:saveEnabled="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end"
        android:text="@string/button_send" />
    <!--red-->
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:orientation="horizontal">

    <EditText
        android:id="@+id/box_4"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:hint="@string/edit_task"
        android:inputType="textCapSentences"
        android:maxLength="32"
        android:padding="10dp"
        android:saveEnabled="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end"
        android:text="@string/button_send" />
    <!--red-->
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:orientation="horizontal">

    <EditText
        android:id="@+id/box_5"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:hint="@string/edit_task"
        android:inputType="textCapSentences"
        android:maxLength="32"
        android:padding="10dp"
        android:saveEnabled="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end"
        android:text="@string/button_send" />
    <!--red-->
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:orientation="horizontal">

    <EditText
        android:id="@+id/box_6"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:hint="@string/edit_task"
        android:inputType="textCapSentences"
        android:maxLength="32"
        android:padding="10dp"
        android:saveEnabled="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end"
        android:text="@string/button_send" />
    <!--red-->
</LinearLayout>

如您所说,需要更多工作的更好解决方案是使用 screenHeight/7 计算高度

【讨论】:

  • 谢谢!稍后我会弄清楚计算部分以获得更好的性能。
【解决方案2】:

有两个主要原因:
1. 在包含EditText和Button的Linearlayout中设置android:layout_weight="1"
2. 在此 EditText 中设置android:layout_height="match_parent",使其与父级一样高。

然后布局代码将是这样的:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:orientation="horizontal">

    <EditText
        android:id="@+id/box_0"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:saveEnabled="true"
        android:inputType="textCapSentences"
        android:hint="hello"
        android:maxLength="32"
        android:padding="10dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="test"
        android:layout_gravity="end|center_vertical" />
    <!--red-->
</LinearLayout>

<EditText
    android:id="@+id/box_1"
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:saveEnabled="true"
    android:inputType="textCapSentences"
    android:maxLength="32"
    android:padding="10dp" />

<EditText
    android:id="@+id/box_2"
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:saveEnabled="true"
    android:inputType="textCapSentences"
    android:maxLength="32"
    android:padding="10dp" />

<EditText
    android:id="@+id/box_3"
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:saveEnabled="true"
    android:inputType="textCapSentences"
    android:maxLength="32"
    android:padding="10dp" />

<EditText
    android:id="@+id/box_4"
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:saveEnabled="true"
    android:inputType="textCapSentences"
    android:maxLength="32"
    android:padding="10dp" />

<EditText
    android:id="@+id/box_5"
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:saveEnabled="true"
    android:inputType="textCapSentences"
    android:maxLength="32"
    android:padding="10dp" />

<EditText
    android:id="@+id/box_6"
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:saveEnabled="true"
    android:inputType="textCapSentences"
    android:maxLength="32"
    android:padding="10dp" />

还有预览图:
希望对你有帮助!

【讨论】:

    【解决方案3】:

    试试这个:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">
        <EditText android:id="@+id/box_0"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:hint="@string/edit_task"
            android:saveEnabled="true"
            android:inputType="textCapSentences"
            android:maxLength="32"
            android:padding="10dp"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="@string/button_send"
            android:layout_gravity="end"/>
        <!--red-->
        </LinearLayout>
    

    你应该让你的horizontal Linear Layout 与其他Edit text 匹配,这就是为什么我将layout_height 更改为0dp 并且我还添加了1 的layout weight。假设他与你的其他@ 相同987654326@s。我还将Edit Text 中的layout_height 更改为"match_parent"。所有其余代码应保持不变。

    【讨论】: