Android Layout_weight

android, android-linearlayout, android-relativelayout

Solution

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

Problem

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 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 is it a bug or maybe I really misunderstood the whole thing about layout_weight?

Original source