Android列表项:文本视图在左侧,图像在右侧

Android列表项:文本视图在左侧,图像在右侧,android,android-layout,Android,Android Layout,我正在和android列表视图中创建一个自定义列表项。它非常简单,左边是文本视图,右边是图像。图像应该一直向右对齐,文本视图应该占据左侧的所有空间。问题是,我可以让图像显示出来,但它会向左推。如果我将textview宽度设置为fill_parent,图像将消失。以下是我的布局: <?xml version="1.0" encoding="utf-8" ?> <LinearLayout xmlns:android="http://schemas.android.com/apk/r

我正在和android列表视图中创建一个自定义列表项。它非常简单,左边是文本视图,右边是图像。图像应该一直向右对齐,文本视图应该占据左侧的所有空间。问题是,我可以让图像显示出来,但它会向左推。如果我将textview宽度设置为fill_parent,图像将消失。以下是我的布局:

<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" 
              android:layout_width="fill_parent" android:layout_height="50px" android:layout_gravity="center_vertical">
  <!-- item -->
  <TextView android:id="@+id/txtItem" android:gravity="center_vertical" android:textSize="20px" android:layout_width="fill_parent" 
            android:layout_height="fill_parent"/>

  <!-- disclosure image -->
  <ImageView android:layout_gravity="right|center_vertical" android:src="@drawable/common_icon_arrow_disclosure" 
              android:layout_width="wrap_content" android:layout_height="wrap_content"/>
</LinearLayout>

我也尝试过相对布局,但同样的情况也发生了。我做错了什么


编辑:为了澄清,以下是我试图实现的目标:


我希望文本字段占据整个左侧区域,图像位于右侧,不缩放,垂直居中。

相对布局就是您想要的。在您的例子中,fill_parent属性导致文本视图将图像推离屏幕。如果使用相对布局,并将文本视图设置为使用布局,则它应能正常工作

这里有一个我认为会起作用的快速示例(我还没有测试出来,它来自记忆,但想法就在那里)


您可以像这样使用位图绘制(这也会添加圆角):


并将此drawable置于TextView的背景属性中:

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/right_image"
    android:text="some text"
    />

将文本与父对象左对齐,并将图像的重力设置为右:

<?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="48dp">

  <TextView
  android:id="@+id/team_member_name"
  android:layout_width="wrap_content"
  android:layout_height="fill_parent"
  android:layout_alignParentLeft="true"
  android:gravity="center_vertical"
  android:singleLine="true"/>

  <ImageView
  android:id="@+id/team_member_icon"
  android:layout_alignParentRight="true"
  android:layout_width="wrap_content"
  android:layout_height="fill_parent"
  android:layout_gravity="right"
  android:layout_marginLeft="8dp"
  android:layout_marginRight="8dp"
  android:src="@drawable/circle_green"/>

</RelativeLayout>

这对我很有用:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

    <TextView android:id="@+id/date"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:layout_marginRight="10dp"
    android:textSize="16sp"
    android:textColor="@color/text_color"
    android:layout_alignParentLeft="true"
    android:gravity="center_vertical"/>

    <ImageView android:id="@+id/imageBtn"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:contentDescription="@string/delete"
    android:src="@drawable/delete_minus_png"
    android:layout_alignParentRight="true" />

</RelativeLayout>

行android:layout_marginRight=“10dp”成功了


希望有帮助

看起来好像有一些布局错误。如果TextView具有fill_parent标志,则即使TextView内容足够小,ImageView也会被推离布局。

您也可以像这样直接将可绘制内容添加到TextView

<TextView
    android:id="@+id/txtItem"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:drawableRight="@drawable/common_icon_arrow_disclosure"
    android:gravity="center_vertical"
    android:textSize="20px" />


这也不行。箭头图像显示SUP,但文本不显示。此外,箭头(比项目低)不是垂直居中,而是顶部对齐。我实际上有一些非常相似的东西,但它有一个带有文本图像布局的imageView,但这与您的示例正好相反,文本显示了,但没有箭头。很抱歉,我没有时间写一个测试用例,但您可以尝试的其他东西是布局左对齐,而不是布局左对齐。我知道我已经完成了你的要求,但我在我的项目中找不到,我知道它使用了RelativeLayout。祝你好运,如果可以的话,我明天会回来查看。我在imageView上使用了layout_alignParentRight=“true”(我切换了图像视图和文本视图,因为图像视图在右侧,所以它确实应该被声明为第二)。这个问题是双重的;我可以显示文本视图和img视图,但是1)图像视图不是垂直居中的-正如我提到的,图像实际上小于行高,2)图像视图没有向右对齐,而是紧跟在文本末尾之后。如果我将文本视图更改为具有布局\u width=“fill\u parent”,图像视图将再次消失:(谢谢!这也适用于为微调器控件设置自定义背景和向下箭头图标。只需将微调器的背景设置为这样定义的可绘制资源,您就可以进行设置。这应该是可以接受的答案,因为TextView会自动将复合可绘制设置在其边界附近。但是我们如何执行onClick action仅在可绘制图像上打开?而不是整个文本?真不敢相信我以前从未找到过。谢谢!textView.SetCompoundDrawableswithintrisicBounds(0,0,R.drawable.common_图标_箭头_,0)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

    <TextView android:id="@+id/date"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:layout_marginRight="10dp"
    android:textSize="16sp"
    android:textColor="@color/text_color"
    android:layout_alignParentLeft="true"
    android:gravity="center_vertical"/>

    <ImageView android:id="@+id/imageBtn"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:contentDescription="@string/delete"
    android:src="@drawable/delete_minus_png"
    android:layout_alignParentRight="true" />

</RelativeLayout>
<TextView
    android:id="@+id/txtItem"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:drawableRight="@drawable/common_icon_arrow_disclosure"
    android:gravity="center_vertical"
    android:textSize="20px" />