在中间修剪TextView,保留结束字符

时间:2014-08-31 13:11:31

标签: android textview

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

asd1234 | september 1

如果左侧textview附加了更多字符(“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>

1 个答案:

答案 0 :(得分:2)

我在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,这有所不同。直到那时我还没有得到正确的输出:

这使我的输出为:

enter image description here

希望这有帮助。

相关问题