Button weight in linear layout

5.7k views Asked by At

This the code I have

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

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.75">

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

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="New "
                    android:id="@+id/button"
                    android:layout_weight="1"
                    android:background="#ffffffff" />

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="New "
                    android:id="@+id/button2"
                    android:layout_weight="1"
                    android:background="#ffffffff" />

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="New "
                    android:id="@+id/button3"
                    android:layout_weight="1"
                    android:background="#ffffffff" />
            </LinearLayout>

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

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="New "
                    android:id="@+id/button5"
                    android:layout_weight="2"
                    android:background="#ffffffff" />

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="New "
                    android:id="@+id/button7"
                    android:layout_weight="1"
                    android:background="#ffffffff" />
            </LinearLayout>

        </LinearLayout>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="0.25"
            android:text="New "
            android:id="@+id/button4"
            android:background="#ffffffff" />
    </LinearLayout>

And this is how it looks:

buttons

I want the first This's width be as the first two New and the second This's width be as the third New

So it looks like this:

buttons2

The only way I can do that is by putting the weights on 0.93/0.07

Shouldn't it work with weights 2/1 as the code above is?

2

There are 2 answers

0
hungryghost On BEST ANSWER

Try leaving out android:weightSum="3" for your second row of buttons. When you omit weightSum, the weights of the views are totaled by default and they essentially become straight proportions (ratios like 2 to 1). So, just using weight=2 for first button (id button5), then weight=1 for second button (id button7) should work.

Also, when using layout_weight, make sure that your resizable views have layout_width or layout_height equal to zero.

Try this xml:

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

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.75">

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

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="New "
                android:id="@+id/button"
                android:layout_weight="1"
                android:background="#ffffffff" />

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="New "
                android:id="@+id/button2"
                android:layout_weight="1"
                android:background="#ffffffff" />

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="New "
                android:id="@+id/button3"
                android:layout_weight="1"
                android:background="#ffffffff" />
        </LinearLayout>

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

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="New "
                android:id="@+id/button5"
                android:layout_weight="2"
                android:background="#ffffffff" />

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="New "
                android:id="@+id/button7"
                android:layout_weight="1"
                android:background="#ffffffff" />
        </LinearLayout>

    </LinearLayout>

    <Button
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.25"
        android:text="New "
        android:id="@+id/button4"
        android:background="#ffffffff" />
</LinearLayout>
2
Jorge Casariego On

This is how I write to have the layout you want

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_weight="1">

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

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="New "
                android:id="@+id/button1"
                android:layout_weight="1"
                android:background="#ffffffff" />

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="New "
                android:id="@+id/button2"
                android:layout_weight="1"
                android:background="#ffffffff" />

        </LinearLayout>

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

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="New "
                android:id="@+id/button4"
                android:layout_weight="2"
                android:background="#ffffffff" />
        </LinearLayout>

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
        <Button
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:text="New "
            android:id="@+id/button5"
            android:layout_weight="1"
            android:background="#ffffffff" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:text="New "
            android:id="@+id/button6"
            android:layout_weight="1"
            android:background="#ffffffff" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="6"
        >
        <Button
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:text="New "
            android:id="@+id/button7"
            android:layout_weight="1"
            android:background="#ffffffff" />

    </LinearLayout>

</LinearLayout>

And this is the result

enter image description here

There is only one thing I'm not sure and is why in my last LinearLayout I had to put weight = 6 to have the button in that height. I'm looking for the reason :/