Uneven weight distribution LinearLayout

I have a LinearLayout that has four views laid out horizontally. The first and last components are the specified size. For two internal views, I just want to share the available 50:50 space. I set each weight to “1,” but when I view the views, the views vary in size depending on the content they store. Screen shot of the layout. The data is hidden but it shows how offset the views are

Here is my xml layout 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 are not actual column names, but I changed them for privacy purposes. This layout is used by ListView, which changes the text of each view as any value. The name and description fields should line up, as they are given 50% of the remaining screen, but when the name is longer, the description is shifted to the right. Why?

+4
source share
2 answers

For the weight to be considered, the layout size should 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 up to 1 (and use fractions) or 100.

So, instead of 1, you should use 50 or .5 for each view. The LinearLayout code will work correctly with any amount of weight, but it becomes difficult if you want to change your presentation later using additional sections.

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

+10
source

Try using android:layout_width="fill_parent" instead of "wrap_content" for all LinearLayout . Or better yet, create 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 may also work, but you need to experiment a bit (I believe that using a nested layout, as in my example, will do the job).

+1
source

All Articles