LISTVIEW ROW GROW - نص محاذاة يسار ، ورمز محاذاة يميني
-
22-09-2019 - |
سؤال
أحاول الحصول على صف ListView لتبدو كما يلي:
| Text-Text-Text <ImageButton> |
مع 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>