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

当前回答

        <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 = "结束" android: maxLines =“1”

约束布局中的textview的完整代码:

<TextView
        android:layout_width="75dp"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:maxLines="1"
        android:textSize="15sp"
        android:textAllCaps="false"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="parent" />

你只要改变…

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>

代码:

TextView your_text_view = (TextView) findViewById(R.id.your_id_textview);
your_text_view.setEllipsize(TextUtils.TruncateAt.END);

xml:

android:maxLines = "5"

e.g.

在马太福音第13章,门徒问耶稣为什么用比喻对众人说话。耶稣回答说、天国的奥秘、只赐给你们、叫你们知道。只是没有赐给他们。

输出: 在马太福音第13章,门徒问耶稣为什么用比喻对众人说话。他回答说:“交给你们是要你们知道……

为了使用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中。

通过这种方式,你可以设置视图的最大长度,同时,你将在结尾有点(“num”是dp的数量):

android:maxWidth="{num}dp"
android:ellipsize="end"