Стиль строки ListView — текст с выравниванием по левому краю и значок с выравниванием по правому краю
-
22-09-2019 - |
Вопрос
Я пытаюсь заставить строку ListView выглядеть следующим образом:
| Text-Text-Text <ImageButton> |
Кнопка изображения привязана к правому краю.Как я могу это сделать?Вот текущий код макета, который я использую.Что я делаю не так?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layercontainer"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#699">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="left">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="YO HOW SI IT GOESSDA" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="right">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/trash" />
</LinearLayout>
</LinearLayout>
Мой код в настоящее время выдает это:
Решение
Шаг 1:Использовать RelativeLayout
база.
Шаг 2:Положите вашу ImageButton
как имеющий android:layout_alignParentRight="true"
Шаг 3:Положите вашу TextView
имеет android:layout_alignParentLeft="true"
, android:layout_toLeftOf="..."
(где ...
это идентификатор вашего ImageButton
), и, возможно, что-то еще RelativeLayout.LayoutParams
значение для вертикального выравнивания
Другие советы
В следующем фрагменте кода представлен пример того, как создать строку ListView, содержащую текст (выровненный по левому краю, через android:layout_alignParentLeft="true"
) и значок (выровненный по горизонтали по правому краю через android:layout_alignParentRight="true"
), и все вертикально по центру (android:layout_centerVertical="true"
).
Он отображается следующим образом (YMMV, в зависимости от глобальных стилей):
Существует также закомментированный дополнительный значок, который можно разместить слева от самого правого значка;удалите теги комментариев XML, чтобы включить.
Вот XML-макет:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:descendantFocusability="blocksDescendants"
android:padding="6dp">
<!-- Note: android:descendantFocusability="blocksDescendants" set to ensure that
OnItemClickListener works by ensuring constituent controls do not take focus -->
<TextView
android:id="@+id/lbl_list_item_row_text"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:lines="1"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="My List Item Text"
/>
<!-- This can be uncommented to add another button
<ImageButton
android:id="@+id/btn_additional_icon"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/icon_additional_icon"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:padding="3dp"
android:background="@null"
android:src="@drawable/icon_additional_icon" />
-->
<ImageButton
android:id="@+id/icon_additional_icon"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="@null"
android:padding="3dp"
android:src="@drawable/icon_right_aligned_icon" />
</RelativeLayout>