我有一个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:layout_width="100dp" <!--your dp or match_parent or 0dp>
android:maxLines="2" <!--count your line>
android:ellipsize="end"

其他回答

您需要为textview的布局添加以下行

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

希望这对你有用。

Use

android:单行模式= " true " android: maxLines = " 1 " 应用:layout_constrainedWidth = " true "

这是我的完整的TextView看起来:

    <TextView
    android:id="@+id/message_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="5dp"
    android:maxLines="1"
    android:singleLine="true"
    android:text="NAME PLACEHOLDER MORE Text"
    android:textColor="@android:color/black"
    android:textSize="16sp"

    android:textStyle="bold"
    app:layout_constrainedWidth="true"
    app:layout_constraintEnd_toStartOf="@id/message_check_sign"
    app:layout_constraintHorizontal_bias="0"
    app:layout_constraintStart_toEndOf="@id/img_chat_contact"
    app:layout_constraintTop_toTopOf="@id/img_chat_contact" />

如。你可以使用

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"

以下是我通过使用各种选项来迫使TextView变为一行(带和不带三个点)。

android:maxLines=“1”

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:maxLines="1"
    android:text="one two three four five six seven eight nine ten" />

这只是强制文本为一行。任何额外的文本都被隐藏。

相关:

android: maxLines android:singleLine(注意这个和这个) android:行

椭圆大小=“结束”

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:maxLines="1"
    android:ellipsize="end"
    android:text="one two three four five six seven eight nine ten" />

这将删除不合适的文本,但通过添加省略号(三个点)让用户知道文本已被截断。

相关:

ellipsize =“开始”(…aaabbbccc) ellipsize =“中产”(aaa…ccc) android:省略号,选项的意思

椭圆大小=“选框”

<TextView
    android:id="@+id/MarqueeText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:maxLines="1"
    android:singleLine="true"
    android:ellipsize="marquee"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:text="one two three four five six seven eight nine ten" />

这使得文本在TextView中自动滚动。注意,有时需要在代码中设置:

textView.setSelected(true);

据说android:maxLines="1"和android:singleLine="true"应该做基本上相同的事情,因为singleLine显然是不赞成的,我宁愿不使用它,但当我把它拿出来,字幕不再滚动。但是,去掉maxLines并不会影响它。

相关:

Android中的选框文本

HorizontalScrollView with scrollhorizontal

<HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/horizontalScrollView">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxLines="1"
        android:scrollHorizontally="true"
        android:text="one two three four five six seven eight nine ten" />
</HorizontalScrollView>

这允许用户手动滚动以查看整行文本。

我正在使用horizontal Recyclerview。 1)这里在CardView, TextView得到扭曲垂直时使用

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

检查粗体TextViews Wyman Group, Jaskolski…

2)但是当我使用singleLine和ellipsize -

android:ellipsize="end"
android:singleLine="true"

检查粗体TextViews Wyman Group, Jaskolski…

第二种解决方案对我来说很有效(使用singleLine)。我也在操作系统版本:4.1及以上(直到8.0)进行了测试,它工作得很好,没有任何崩溃。