我正在努力解决 textview 重叠问题。我有两个并排的文本 View

asd1234 | september 1

如果左文本 View 附加了更多字符(“567”),结果是
asd…23{lost chars} | september 1

想要的结果是
asd…567 | september 1

我的 textview 属性是
        android:ellipsize="middle"
        android:maxLines="1"
        android:scrollHorizontally="true"

有什么方法可以使它与属性一起使用,还是必须以编程方式修剪字符串?正确的做法是什么?谢谢

编辑
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/background_dark"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/header"
        style="?android:attr/listSeparatorTextViewStyle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/date"
        android:layout_centerVertical="true"
        android:maxLines="1"
        android:textColor="@android:color/holo_blue_dark" />

    <TextView
        android:id="@+id/date"
        style="?android:attr/listSeparatorTextViewStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:maxLines="1"
        android:textColor="@android:color/holo_blue_dark" />

</RelativeLayout>

最佳答案

我对您的 xml 进行了一些更改:

<?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"
    android:background="@android:color/background_dark"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/header"
        style="?android:attr/listSeparatorTextViewStyle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/date"
        android:singleLine="true"
        android:text="I have wrote some longestttt text over here for testing the ellipsize"
        android:textColor="#fff"
        android:ellipsize="middle"/>

    <TextView
        android:id="@+id/date"
        style="?android:attr/listSeparatorTextViewStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:maxLines="1"
        android:text="02-09-2014"
        android:textColor="#fff" />

</RelativeLayout>

主要是将 maxLines = 1 更改为 singleLine = true ,这有所不同。直到那时我没有得到正确的输出:

这给了我输出:

希望这可以帮助。

10-07 12:45
查看更多