LinearLayout layout_weight

I have a ListView that is populated with strings. These lines are taken from an XML file that looks like this:

<?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 and columnB elements to be very close together, so my rows are evenly distributed in three parts, that is: columnA, (columnB + columnC), and then columnD. I tried to achieve this using layout_weight, as you can see, however the code above seems to have the opposite effect. columnA and columnB are very much twisted on the left, column C seems to float in large space by itself, and then columnD is located close to column C, with too much space on the right. What am I doing wrong ?: S

+1
source share
2 answers

I would think that you want columnC and columnB to have the same weight, but you set 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 that is how you can get the desired result.

If you still have problems with this, it can help us help you if you can post a screenshot of how it looks and how you want it to look.

0
source

Give it a try. You can basically determine how much space each view will β€œwant” by weight / total weight. You do not need to try to make them equal to 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"/> 
+2
source

Source: https://habr.com/ru/post/908310/


All Articles