如何在Android List Item的List项目中实现ellipsize(三个点)

时间:2014-05-19 19:49:49

标签: android android-layout listview

我的ListView列表项布局如下:同时附加显示此布局Output Image of Layout 输出的图像。
在上面的输出图像中,我们可以通过右对齐的TextView看到第二行中出现的文本。我希望在文本的末尾放置3个点,不重叠。

  <?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent" >

<TextView
    android:id="@+id/name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="15dp"
    android:layout_marginTop="16dp"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/sname"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/name"
    android:layout_below="@+id/name"
    android:layout_marginTop="15dp"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/abbr"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/sname"
    android:layout_alignParentRight="true"
    android:layout_marginRight="15dp"
    android:text="Small Text"
    android:textAppearance="?android:attr/textAppearanceSmall" />

    </RelativeLayout>


任何人都可以帮我解决这个问题吗?
感谢Adavnce

1 个答案:

答案 0 :(得分:3)

只需为TextView添加这些布局属性

即可
android:ellipsize="end"
android:maxLines="1"

这告诉android你希望你的textview在最后被椭圆化(在文本的末尾添加3个点)并且你不希望你的文本换行到下一行(通过设置最大行)

您唯一需要做的就是确保TextView不重叠(cri ...部分)。有很多方法可以做到这一点。

  1. 将您的abbr TextView移动到其他TextView上方并将android:layout_toLeftOf="@id/abbr"添加到非abbr TextViews

  2. 通常重做您的布局以确保没有重叠,使用LinearLayouts可以帮助实现这一目标,例如重量。

  3. 知道窗口大小后动态设置宽度。