我有一个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"/>

当前回答

你可以限制你的文本视图的字符数量,并在文本后添加(…)。 假设你只需要显示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

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

其他回答

        <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"/>

我认为你给固定的高度和宽度的文本视图。那么你的解决方案就会起作用。

除了

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

你应该设置

android:layout_width="0dp"

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

你只要改变…

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属性,你必须限制TextView的布局宽度,这样文本就超出了TextView视图的边界。

所以,android:layout_width属性在这里起着关键作用,相应地设置它。

一个例子可以是:

<TextView        
    android:layout_width="120dp"
    android:layout_height="wrap_content"
    android:ellipsize="end"
    android:text="This is a very long text to be displayed"
    android:textSize="12sp"
    android:maxLines="1"            
     />

现在,这里如果文本在android:text="这是一个很长的文本要显示"从TextView与android:layout_width="120dp", android:ellipsize="end"将截断文本和地方…(3点)在它后面。这太长了……将显示在TextView中。