ggrr
ggrr

Reputation: 7867

Why is the effect of layout_weight in View behaves differently from TextView?

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):

enter image description here

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):

enter image description here

what is the reason?

Upvotes: 2

Views: 93

Answers (4)

Subarata Talukder
Subarata Talukder

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" />

Actual output by using Wiew

Upvotes: 0

Pratik Popat
Pratik Popat

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

King of Masses
King of Masses

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

Arun Shankar
Arun Shankar

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

Related Questions