Reputation: 7867
I tried to set 3 colored areas in 1:2:3 ratio using TextView:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:layout_weight="1"
android:background="#FF0000"
android:layout_width="wrap_content"
android:layout_height="match_parent"
/>
<TextView
android:layout_weight="2"
android:background="#00FF00"
android:layout_width="wrap_content"
android:layout_height="match_parent"
/>
<TextView
android:layout_weight="3"
android:background="#0000FF"
android:layout_width="wrap_content"
android:layout_height="match_parent"
/>
</LinearLayout>
which has my desired result (screen shot by android studio preview):
but when I change TextView into View:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<View
android:layout_weight="1"
android:background="#FF0000"
android:layout_width="wrap_content"
android:layout_height="match_parent"
/>
<View
android:layout_weight="2"
android:background="#00FF00"
android:layout_width="wrap_content"
android:layout_height="match_parent"
/>
<View
android:layout_weight="3"
android:background="#0000FF"
android:layout_width="wrap_content"
android:layout_height="match_parent"
/>
</LinearLayout>
the layout changes (screen shot by android studio preview):
what is the reason?
Upvotes: 2
Views: 93
Reputation: 6331
Your parent layout orientation is horizontal, so you can not use layout_width either match_parent, wrap_content or any value other than 0. So why it is giving unwanted result.
For more information, please read Layout Weight section.
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#FF0000" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:background="#00FF00" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:background="#0000FF" />
Upvotes: 0
Reputation: 3009
Replace width with 0dp.
<?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">
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#FF0000" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:background="#00FF00" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:background="#0000FF" />
</LinearLayout>
Upvotes: 0
Reputation: 18775
You have to try like this
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<View
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#FF0000"/>
<View
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#00FF00" />
<View
android:layout_weight="3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#0000FF"/>
</LinearLayout>
Upvotes: 1
Reputation: 2295
It has got to do with the width. Given that its a horizontal LinearLayout with match parent as the LinearLayout's width, and each of your views(View/TextView) is been given weights, the individual widths of the views are causing the misbehaviour.
If you gave each of the views width to be 0dp, both will behave in the same way
Upvotes: 0