Расположение элементов списка Android

1

У меня есть простая реализация 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)                 |
|      |                                    |
+------+------------------------------------+
Теги:
android-layout

3 ответа

1
Лучший ответ

Лучший вариант - изменить макет в относительный макет.

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

Z2 должен быть слева от Z1 и выровнен с родительским верхом.

Z3 следует выровнять с Z2 и ниже Z2.

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

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

1

я думаю, вам нужно ознакомиться с относительной компоновкой.

<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

Я бы использовал 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>

Ещё вопросы

Сообщество Overcoder
Наверх
Меню