LinearLayout layout_weight - PullRequest
       16

LinearLayout layout_weight

1 голос
/ 05 июля 2011

У меня есть ListView, который заполнен строками. Эти строки взяты из файла XML, который выглядит следующим образом:

 <?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"/>

Проблема в том, что я хочу, чтобы элемент columnC и элемент columnB были очень близко друг к другу, чтобы мои строки были равномерно разделены на три части, т.е. columnA, (columnB + columnC) и затем columnD. Я пытался добиться этого, используя layout_weight, как вы можете видеть, однако приведенный выше код, похоже, имеет обратный эффект. columnA и columnB слева очень сжатые, столбец C кажется плавающим в большом пространстве сам по себе, а затем columnD располагается близко к columnC, с слишком большим количеством места справа. Что я делаю неправильно? : S

Ответы [ 2 ]

2 голосов
/ 05 июля 2011

Дайте это попробовать. Вы можете в принципе вычислить, сколько места будет «хотеть» каждый вид, по весу / общему весу. Вам не нужно пытаться сделать их равными 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"/>
0 голосов
/ 05 июля 2011

Я бы подумал, что вы хотите, чтобы столбцы C и columnB имели одинаковый вес, но вы должны задать для них разные значения.

Попробуйте

columnA: weight = 2

columnB: вес = 1

columnC: вес = 1

columnD: вес = 2

У меня очень ограниченный опыт работы с атрибутом веса, но я думаю, что именно так выможет получить желаемый результат.

Если у вас все еще есть проблемы, мы можем помочь вам, если вы можете опубликовать снимок экрана с тем, как он выглядит и как вы хотите, чтобы он выглядел.

...