1

I have a ListView that is populated with rows. These rows come from an XML file that looks like:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingLeft="8dip" android:weightSum="100"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:id="@+id/columnA" android:layout_weight="30" android:layout_gravity="center"/> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:id="@+id/columnB" android:layout_weight="30" android:layout_gravity="center"/> <ImageView android:layout_width="0dp" android:layout_height="wrap_content" android:src="@drawable/icon" android:id="@+id/columnC" android:layout_weight="10" android:layout_gravity="center" > </ImageView> <CheckBox android:layout_width="0dp" android:layout_height="wrap_content" android:id="@+id/columnD" android:layout_weight="30" android:layout_gravity="center"/> 

The problem is that I want the columnC item and columnB item to be very close together, so that my rows are equally spaced out in three parts, i.e.: columnA, (columnB+columnC) and then columnD. I tried to achieve this by using layout_weight as you can see, however the above code seems to have the opposite effect. columnA and columnB are very squished on the left, column C seems to be floating in a large space on its own, and then columnD is located close to columnC, with too much space on its right. What am I doing wrong? :s

2 Answers 2

2

Give this a try. You can basically figure how much space each view will "want" by weight/total weight. You don't have to try to make them equal 100.

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingLeft="8dip" android:weightSum="100"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:id="@+id/columnA" android:layout_weight="2" android:layout_gravity="center"/> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:id="@+id/columnB" android:layout_weight="1" android:layout_gravity="center"/> <ImageView android:layout_width="0dp" android:layout_height="wrap_content" android:src="@drawable/icon" android:id="@+id/columnC" android:layout_weight="1" android:layout_gravity="center" > </ImageView> <CheckBox android:layout_width="0dp" android:layout_height="wrap_content" android:id="@+id/columnD" android:layout_weight="2" android:layout_gravity="center"/> 
Sign up to request clarification or add additional context in comments.

Comments

0

I would think that you want columnC and columnB to have the same weight, but you have it set with them different.

Try

columnA:weight=2

columnB:weight=1

columnC:weight=1

columnD:weight=2

I have very limited experience with the weight attribute but I think this is how you can get your desired result.

If you're still having trouble with it, may help us help you if you can post a screen shot of how it looks, and how you want it to look.

4 Comments

Beat me to it, didn't realize by how much too... didn't get the "Load 1 new answer" till just a moment before I posted.
Thanks to both of you, this definately looks better, however columnD is still appearing too close to columnC, with too much space on the right :s May be something to do with the fact that its a checkbox.
okay - turns out I had to wrap my checkbox in a linearlayout to fix this remaining problem, and then set the weight and gravity in the linear layout. Thx!
You could also just use layout_marginLeft on the checkbox to move it over to the right a bit.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.