Расположение элементов списка Android - PullRequest
1 голос
/ 24 августа 2011

У меня есть простая реализация ListActivity, которая имеет следующий дизайн

+------+------------------------------------+
|  Z1  |  Z2                                |
|      |                                    |
+------+------------------------------------+

, где Z1 является элементом ImageView, а Z2 - элементом TextView.Это моя разметка

<?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"
    android:orientation="horizontal">
    <ImageView android:id="@+id/icon" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:src="@drawable/icon" />
    <TextView android:id="@+id/sectionTitle" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:textStyle="bold" />
</LinearLayout>

Как изменить мою разметку, чтобы получить дизайн, который добавляет еще один элемент, как в следующем примере?

+------+------------------------------------+
|  Z1  |  Z2                                |
+      +------------------------------------+
|      |  Z3 (New TextView)                 |
|      |                                    |
+------+------------------------------------+

Ответы [ 3 ]

1 голос
/ 24 августа 2011

Я бы использовал LinearLayout, но с вложенным LinearLayout с TextView в нем.Вот пример XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="horizontal">
    <ImageView android:id="@+id/catch_image"
               android:layout_width="60dip"
               android:layout_height="60dip"/>
    <LinearLayout android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:orientation="vertical"
                  android:layout_weight="1">
        <TextView android:id="@+id/catch_species"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"/>
        <TextView android:id="@+id/catch_datetime"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"/>
    </LinearLayout>
</LinearLayout>
1 голос
/ 24 августа 2011

Полагаю, вам нужно ознакомиться с относительным расположением.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content">

    <ImageView android:id="@+id/icon" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="25dip"
    />
    <TextView android:id="@+id/title"
        android:layout_toRightOf="@id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_alignTop="@+id/icon"     
    />
    <TextView android:id="@+id/description"
        android:layout_toRightOf="@id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_alignBottom="@+id/icon"      
    />
</RelativeLayout> 

как то так будет работать.

1 голос
/ 24 августа 2011

Наилучшим вариантом будет изменение вашего макета в относительный макет.

Заставьте Z1 выровнять родительский элемент влево сверху и снизу и укажите его гравитацию по центру, и по центру по вертикали (в пределах родительского элемента).

Z2 должен быть слева от Z1 и выровнен по верху родителя.

Z3 должен быть выровнен по левому краю с Z2 и ниже Z2.

Все эти "правила" Iиспользуемые указаны в относительной компоновке.

Дополнительная информация: Документация RelativeLayout и Пример учебного пособия .

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...