我有一个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:maxLength="13"

这将限制textview长度为13,但问题是,如果你试图添加3点(…),它不会显示它,因为它将是textview长度的一部分。

     String userName;
     if (data.length() >= 13) {
            userName = data.substring(0, 13)+ "...";

     } else {

            userName = data;

    }
        textView.setText(userName);

除此之外,你必须使用

 android:maxLines="1"

其他回答

你只要改变…

android:layout_width="wrap_content"

使用下面这行

android:layout_width="match_parent"

……

   <LinearLayout
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_centerVertical="true"
       android:layout_marginLeft="10dp"
       android:layout_marginTop="10dp"
       android:layout_toRightOf="@+id/visitBox"
       android:orientation="vertical" >

             <TextView
                  android:id="@+id/txvrequestTitle"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:singleLine="true"
                  android:text="Abcdefghighiklmnon"
                  android:textAppearance="? 
                  android:attr/textAppearanceLarge"
                  android:textColor="@color/orange_color" />

   </LinearLayout>

除了

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

你应该设置

android:layout_width="0dp"

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

设置android:maxLength="8"在Textview

如果你想设置你的字符串>5。然后设置textview长度5+3 (3 -dot)

if (yourString.length()>5) // 
    {
        textview.setText(yourString.substring(0,5)+"...");
    }
    else {
        textview.setText(title);
    }

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

android:singleLine="true"

通过使用,我得到了预期的结果

android:maxLines="2"
android:minLines="2"
android:ellipsize="end"

诀窍是将maxLines和minLines设置为相同的值…和不只是android:lines = "2",不做的把戏。 此外,您还避免了任何弃用的属性。