Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uneven LinearLayout weight distribution

I have a LinearLayout that has four views layed out horizontally. The first and last component are a set size. For the inner two views I want to just share the available space 50:50. I set each to a weight of "1" but when the views are layed out, the views are different sizes depending on the content they hold. Screen shot of the layout. The data is hidden but it shows how offset the views are

Here is my layout xml for reference.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content">
    <ImageView 
        android:id="@+id/status" 
        android:src="@drawable/white"
        android:paddingRight="10dip" 
        android:layout_height="35dip" 
        android:layout_width="35dip">
    </ImageView>
    <TextView android:id="@+id/name" 
        android:text="Name" 
        android:layout_height="fill_parent" 
        android:layout_toRightOf="@id/status" 
        android:layout_width="wrap_content" 
        android:layout_weight="1" 
        android:textSize="25dip">
    </TextView>
    <TextView android:id="@+id/description"
        android:text="Description"
        android:layout_toRightOf="@id/name" 
        android:layout_height="fill_parent"
        android:layout_width="wrap_content"
        android:layout_weight="1"
        android:textSize="25dip">
    </TextView>
    <TextView android:id="@+id/time" 
        android:text="Time" 
        android:layout_width="wrap_content" 
        android:layout_height="fill_parent"
        android:layout_toRightOf="@id/description" 
        android:textSize="25dip">
    </TextView>
</LinearLayout>

Obviously these aren't the actual column names but I changed them for privacy purposes. This layout is used by a ListView which changes the text of each view to be whatever value its presented. The name and description fields should line up since they're both given 50% of the remaining screen but when the name is longer the description is shifted right. Why?

like image 922
Spidy Avatar asked Dec 16 '22 13:12

Spidy


2 Answers

For the weight to be considered, the layout dimension needs to be 0 (zero)

<TextView android:id="@+id/name" 
    android:text="Name" 
    android:layout_height="fill_parent"  
    android:layout_width="0dip" 
    android:layout_weight="1" 
    android:textSize="25dip">
</TextView>

I also recommend making your weight add up to either 1 (and use fractions) or 100.

So instead of 1 you would use either 50 or .5 for each view. The LinearLayout code will work properly with any weight sum, but it gets difficult if you want to modify your view later with additional sections.

Also, if you are not using relative layout, get rid of the toRightOf attributes. Less is more.

like image 97
slund Avatar answered Jan 05 '23 04:01

slund


Try to use android:layout_width="fill_parent" instead of "wrap_content" in all children of LinearLayout. Or better yet, make such a structure in your xml:

<RelativeLayout>
    <ImageView />       # status, fixed width, alignParentLeft="true"
    <TextView />        # time, fixed width, alignParentRight="true"
    <LinearLayout>      # layout_width="fill_parent", toLeftOf="time" toRightOf="status"
        <TextView />    # name, use layout_weight="1"
        <TextView />    # description, use layout_weight="1"
    </LinearLayout>
</RelativeLayout>

This should do what you want. Using LinearLayout instead of RelativeLayout might work too, but you have to experiment a bit (I believe using nested Layout, as in my example, will do the work).

like image 27
Robert Kolner Avatar answered Jan 05 '23 03:01

Robert Kolner