IKo
IKo

Reputation: 5796

Why views are not equally spread

I have 4 TextViews. What I'm trying to do is to make all guests in the list take equal space (except the 'VIP List' TextView). So I've set layout_height to 0dp and layout_weight to 1 but there is still some place not taken in the bottom. What is wrong with my code?

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/darker_gray">

    <TextView
        android:text="VIP List"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"        
        android:background="#00ACC1"
        android:textSize="24sp"
        android:textStyle="bold"
        android:textColor="#FFFFFF"
        android:gravity="center" />

    <TextView
        android:text="Bob"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#BF360C"
        android:textSize="24sp"
        android:textColor="#FFFFFF"/>

    <TextView
        android:text="Bill"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#4CAF50"
        android:textSize="24sp" 
        android:textColor="#FFFFFF"/>

    <TextView
        android:text="Ben"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#2E7D32"
        android:textSize="24sp" 
        android:textColor="#FFFFFF"/>

</LinearLayout>

enter image description here

Upvotes: 1

Views: 49

Answers (3)

NipunPerfect
NipunPerfect

Reputation: 125

It will work.. Use a Linear layout under the parent layout and it android:weightSum="3" and just put the vip list outside of second linear layout Like:

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/darker_gray"
    android:orientation="vertical">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#00ACC1"
    android:gravity="center"
    android:text="VIP List"
    android:textColor="#FFFFFF"
    android:textSize="24sp"
    android:textStyle="bold" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/darker_gray"
    android:weightSum="3"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#BF360C"
        android:text="Bob"
        android:textColor="#FFFFFF"
        android:textSize="24sp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#4CAF50"
        android:text="Bill"
        android:textColor="#FFFFFF"
        android:textSize="24sp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#2E7D32"
        android:text="Ben"
        android:textColor="#FFFFFF"
        android:textSize="24sp" />

</LinearLayout>
</LinearLayout>

Upvotes: 0

AlexTa
AlexTa

Reputation: 5251

Put out "VIP List" TextView from content container LinearLayout because it is not part of "relative" TextView group, then add android:weightSum="3" to LinearLayout content container because you have 3 TextViews with weight = 1. Finally wrap all view within a parent LinearLayout.

Consider something like below:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/darker_gray"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#00ACC1"
        android:gravity="center"
        android:text="VIP List"
        android:textColor="#FFFFFF"
        android:textSize="24sp"
        android:textStyle="bold" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/darker_gray"
        android:weightSum="3"
        android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#BF360C"
        android:text="Bob"
        android:textColor="#FFFFFF"
        android:textSize="24sp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#4CAF50"
        android:text="Bill"
        android:textColor="#FFFFFF"
        android:textSize="24sp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#2E7D32"
        android:text="Ben"
        android:textColor="#FFFFFF"
        android:textSize="24sp" />

    </LinearLayout>
</LinearLayout>

Upvotes: 1

mksengun
mksengun

Reputation: 145

Change your LinearLayout code with this;

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="3"
    android:background="@android:color/darker_gray">
...
</LinearLayout>

Upvotes: 3

Related Questions