Perhaps I misunderstood the layout_weight parameter, but I cannot understand why something like this does not work ...
<?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 text views inside LinearLayout. I want to place them in percentage-wide columns. And this is what it produces:
With layout_weight set to 0.3 / 0.2 / 0.5

Then I changed the layout_weight parameters 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 the way I wanted:
With layout_weight set to 0.3 / 0.2 / 0.5

Is this a mistake, or maybe I really misunderstood everything about layout_weight?
source
share