[英]ListView row styling - left aligned text, and right-aligned icon
I'm trying to get ListView row to look like the following: 我正在尝试将ListView行看起来如下所示:
| Text-Text-Text <ImageButton> |
With the imagebutton snapped to the right edge. 图像按钮捕捉到右边缘。 How can I do this?
我怎样才能做到这一点? Here's the current layout code I'm using.
这是我正在使用的当前布局代码。 What am I doing wrong?
我究竟做错了什么?
<?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>
My code currently produces this: 我的代码目前产生这个:
Step #1: Use a RelativeLayout
base. 步骤#1:使用
RelativeLayout
基础。
Step #2: Put your ImageButton
as having android:layout_alignParentRight="true"
第2步:将你的
ImageButton
设为android:layout_alignParentRight="true"
Step #3: Put your TextView
has having android:layout_alignParentLeft="true"
, android:layout_toLeftOf="..."
(where ...
is the ID of your ImageButton
), and perhaps some other RelativeLayout.LayoutParams
value for vertical alignment 第3步:把
TextView
放到android:layout_alignParentLeft="true"
, android:layout_toLeftOf="..."
(其中...
是你的ImageButton
的ID),也许还有其他一些RelativeLayout.LayoutParams
值用于垂直对齐
The following code snippet provides an example of how to create a ListView row that has some text (horizontally left-aligned, via android:layout_alignParentLeft="true"
), and an icon (horizontally right-aligned via android:layout_alignParentRight="true"
), and all vertically centred ( android:layout_centerVertical="true"
). 以下代码片段提供了一个示例,说明如何创建具有一些文本的ListView行(水平左对齐,通过
android:layout_alignParentLeft="true"
)和一个图标(通过android:layout_alignParentRight="true"
水平右对齐android:layout_alignParentRight="true"
),并且所有垂直居中( android:layout_centerVertical="true"
)。
It renders as follows (YMMV, depending on global styles): 它呈现如下(YMMV,取决于全局样式):
There is also a commented-out additional icon that can be placed to the left of the right-most icon; 还有一个注释掉的附加图标,可以放在最右边的图标的左侧; remove the XML comment tags to enable.
删除XML注释标记以启用。
Here is the layout 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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.