Chris
Chris

Reputation: 586

Android Layout_weight

Maybe I misunderstood the layout_weight parameter but I can't get it why something like this is not working...

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

<TextView
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="TextView" android:layout_weight="0.3" android:background="@color/apr_blue"/>

<TextView
    android:id="@+id/textView2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.2"
    android:text="TextView" 
    android:background="@color/apr_brightyellow"/>

<TextView
    android:id="@+id/textView3"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.5"
    android:text="TextView" 
    android:background="@color/apr_yellow"/>

</LinearLayout>

I have three or more textviews within a LinearLayout. I want to place them in columns with a percentage width. And that is what it produces:

With layout_weight setted to 0.3 / 0.2 / 0.5

Android layout_weight incorrectly

Then I varied the layout_weight parameters just a bit as follows:

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

<TextView
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="TextView" android:layout_weight="0.3" android:background="@color/apr_blue"/>

<TextView
    android:id="@+id/textView2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.3"
    android:text="TextView" 
    android:background="@color/apr_brightyellow"/>

<TextView
    android:id="@+id/textView3"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.4"
    android:text="TextView" 
    android:background="@color/apr_yellow"/>

</LinearLayout>

And now I get it as I wanted:

With layout_weight setted to 0.3 / 0.2 / 0.5

Android layout_weight correctly

is it a bug or maybe I really misunderstood the whole thing about layout_weight?

Upvotes: 3

Views: 6242

Answers (2)

Alex Kucherenko
Alex Kucherenko

Reputation: 20307

When you use android:layout_weight you should set android:layout_width="0dp" (in case when vertical orientation - android:layout_height="0dp").

Upvotes: 12

MAC
MAC

Reputation: 15847

enter image description here

try this....

        <TextView
            android:background="#F2e"
            android:layout_weight="3"
            android:id="@+id/textView1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="TextView" />

        <TextView
             android:background="#F6e"
            android:layout_weight="2"
            android:id="@+id/textView2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="TextView" />

        <TextView
             android:background="#F45"
            android:layout_weight="5"
            android:id="@+id/textView3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="TextView" />

    </LinearLayout>

Upvotes: 1

Related Questions