Horizonal LinearLayouts in a Vertical LinearLayout

I want a few EditTexts with buttons on their right. Right now I just have the Edittexts filling the parent with the 0dp trick. If I add the button the height of the EditText will shrink with wrap_content. If I do match_parent one EditText fills the entire screen.

RelativeLayout doesn't work because you can't make it match the parent in the same way as LinearLayout.

I thought of getting the screen height and then setting the layout height to 1/7 that (since I have 7 EditTexts). Plausible?

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

I only did the double LinearLayout in the first EditText.

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

See how the second one is larger than the rest to compensate and the first is smaller?


Why don't you add a ScrollView in there? There will be tons of devices that will never fit correctly 7 editText in the screen.

Anyway it's not raccomended to use nested LinearLayout with weight. But you can achive what you want to do this way.

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

A better solution that require a bit more work is, as you said, calculating the height with screenHeight/7


There are two main reasons:
1. Set android:layout_weight="1" in the Linearlayout which contains a EditText and a Button.
2. Set android:layout_height="match_parent" in this EditText so that it will be as high as it's parent.

And then the layout code will be something like this:

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

And the preview image:
Hope it helps you!


Try this:

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

You should make your horizontal Linear Layout , match to other Edit text , this is why i changed the layout_height to 0dp and I also add the layout weight of 1. It's suppose to be he same to your other Edit text s. Also I changed the layout_height in the Edit Text to "match_parent" . All the rest of the code should remain the same.

链接地址: http://www.djcxy.com/p/84118.html

上一篇: 狮身人面像:模块中的功能列表

下一篇: 垂直LinearLayout中的水平LinearLayouts