Listview-Reihe Styling - links ausgerichteten Text und rechtsbündig Symbol
-
22-09-2019 - |
Frage
Ich versuche, Listview-Reihe wie folgt aussehen zu bekommen:
| Text-Text-Text <ImageButton> |
Mit der Imagebutton an den rechten Rand aufgeschnappt. Wie kann ich das machen? Hier ist der aktuelle Layout-Code Ich verwende. Was mache ich falsch?
<?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>
Mein Code produziert derzeit folgendermaßen aus:
Lösung
Schritt 1:. Verwenden Sie eine RelativeLayout
Basis
Schritt 2: Setzen Sie Ihren ImageButton
als android:layout_alignParentRight="true"
mit
Schritt 3: Setzen Sie Ihre TextView
android:layout_alignParentLeft="true"
hat mit, android:layout_toLeftOf="..."
(wo ...
die ID Ihres ImageButton
ist) und vielleicht einige andere RelativeLayout.LayoutParams
Wert für die vertikale Ausrichtung
Andere Tipps
Das folgende Codefragment zeigt ein Beispiel dafür, wie eine Listview Reihe zu erstellen, die einen Text hat (horizontal linksbündig, über android:layout_alignParentLeft="true"
) und ein Symbol (horizontal rechtsbündig über android:layout_alignParentRight="true"
) und alle vertikal zentrierte (android:layout_centerVertical="true"
) .
Es macht wie folgt (YMMV, je nach globalen Stilen):
Es gibt auch ein kommentierte-out zusätzliches Symbol, das auf dem linken Seite des am weitesten rechts stehenden Symbol platziert werden kann; Entfernen Sie die XML-Kommentar-Tags zu aktivieren.
Hier ist das Layout 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>