我试图让 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>

我的代码目前产生这个:grrr

有帮助吗?

解决方案

步骤#1:使用一个RelativeLayout

步骤#2:把你ImageButton为具有android:layout_alignParentRight="true"

步骤#3:把你TextView具有android:layout_alignParentLeft="true"android:layout_toLeftOf="..."(其中...是你ImageButton的ID),或许为垂直取向一些其它RelativeLayout.LayoutParams

其他提示

以下代码片段提供了如何创建包含一些文本的 ListView 行的示例(水平左对齐,通过 android:layout_alignParentLeft="true")和一个图标(通过水平右对齐 android:layout_alignParentRight="true"),并且所有垂直居中(android:layout_centerVertical="true").

它呈现如下(YMMV,取决于全局样式):

Rendering of code example

还有一个注释掉的附加图标,可以放置在最右侧图标的左侧;删除要启用的 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归因
不隶属于 StackOverflow
scroll top