我有一个TextView,我想限制它的字符。实际上,我可以这样做,但我正在寻找的事情是如何在字符串的末尾添加三个点(…)。这一个显示文本已经继续。这是我的XML,但没有点,尽管它限制了我的文本。

<TextView 
        android:id                      = "@+id/tvFixture"
        android:layout_width            = "wrap_content"
        android:layout_height           = "wrap_content"
        android:layout_toLeftOf         = "@id/ivFixture_Guest"
        android:text                    = "@string/test_06"
        android:lines                   = "1"
        android:ems                     = "3"
        android:gravity                 = "right"
        style                           = "@style/simpletopic.black" 
        android:ellipsize="end"/>

当前回答

除了

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

你应该设置

android:layout_width="0dp"

也称为“匹配约束”,因为wrap_content值只是将方框展开以适应整个文本,而ellipsize属性无法发挥其作用。

其他回答

你可以限制你的文本视图的字符数量,并在文本后添加(…)。 假设你只需要显示5个字母,然后你需要显示(…),只需执行以下操作:

String YourString = "abcdefghijk";

if(YourString.length()>5){

YourString  =  YourString.substring(0,4)+"...";

your_text_view.setText(YourString);
}else{

 your_text_view.setText(YourString); //Dont do any change

}

一个小hack ^_^。虽然这不是一个好的解决方案。但有一项工作对我很有用:D

编辑: 我已经根据你的有限编号增加了对较少字符的检查。的字符。

我认为你想限制宽度为一行,而不是限制它的字符?由于singleLine已弃用,您可以尝试使用以下组合:

android:maxLines="1"
android:scrollHorizontally="true"
android:ellipsize="end"

1.设置静态宽度如7odp

2.使用安卓:省略号=“结束”

3.使用安卓:maxLines=“1”

4.使用安卓:单行=“真”

<TextView
        android:id="@+id/tv_status"
        **android:layout_width="70dp"**
        android:layout_height="wrap_content"
        android:layout_marginEnd="@dimen/padding_8"
        android:gravity="center|center_horizontal"
        android:includeFontPadding="false"
        android:textColor="@color/black_2a"
        android:textSize="@dimen/text_size_1"
        **android:ellipsize="end"
        android:maxLines="1"
        android:singleLine="true"**
        app:layout_constrainedWidth="true"
        app:borrowStatusText="@{item.lenders[0].status}"
        app:layout_constraintEnd_toStartOf="@id/iv_vector"
        app:layout_constraintTop_toTopOf="parent" />

你可以用XML写这一行,在这里你取textview:

android:singleLine="true"
        <TextView
            android:id="@+id/product_description"
            android:layout_width="165dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="2dp"
            android:paddingLeft="12dp"
            android:paddingRight="12dp"
            android:text="Pack of 4 summer printed pajama"
            android:textColor="#d2131c"
            android:textSize="12sp"
            android:maxLines="2"
            android:ellipsize="end"/>