Android,如何限制 TextView 的宽度(并在文本末尾添加三个点)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10748796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:50:07  来源:igfitidea点击:

Android, How to limit width of TextView (and add three dots at the end of text)?

androidtextviewellipsis

提问by Hesam

I have a TextViewthat I want to limit characters of it. Actually, I can do this but the thing that I'm looking for is how to add three dots (...) at the end of string. This one shows the text has continue. This is my XML but there is no dots although it limit my text.

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

回答by Mohammed Azharuddin Shaikh

Deprecated:

弃用:

Add one more property android:singleLine="true"in your Textview

android:singleLine="true"在 Textview 中再添加一项属性

Updated:

更新:

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

回答by Suragch

The following is what I learned by playing around with various options for forcing a TextViewto a single line (with and without the three dots).

以下是我通过TextView尝试将 a强制为单行(带和不带三个点)的各种选项而学到的东西。

enter image description here

在此处输入图片说明

android:maxLines="1"

机器人: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" />

This just forces the text to one line. Any extra text is hidden.

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

Related:

有关的:

ellipsize="end"

省略号=“结束”

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

This cuts off the text that doesn't fit but lets users know that the text has been truncated by adding an ellipsis (the three dots).

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

Related:

有关的:

ellipsize="marquee"

椭圆尺寸=“选框”

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

This makes the text scroll automatically across the TextView. Note that sometimesit needs to be set in code:

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

textView.setSelected(true);

Supposedly android:maxLines="1"and android:singleLine="true"should do basically the same thing and since singleLine is apparently deprecatedI would prefer not to use it, but when I take it out, the marquee doesn't scroll anymore. Taking maxLinesout doesn't affect it, though.

据说android:maxLines="1"并且android:singleLine="true"应该做基本相同的事情,因为 singleLine显然已弃用,我宁愿不使用它,但是当我取出它时,选框不再滚动。不过取maxLines出来不影响。

Related:

有关的:

HorizontalScrollView with scrollHorizontally

Horizo​​ntalScrollView 与 scrollHorizo​​ntally

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

This allows the user to manually scroll to see the whole line of text.

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

回答by Niranj Patel

Try this property of TextView in your layout file..

在您的布局文件中尝试 TextView 的这个属性。

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

回答by Muz

I take it you want to limit width to one line and not limit it by character? Since singleLineis deprecated, you could try using the following together:

我认为您想将宽度限制为一行而不是按字符限制吗?由于singleLine已弃用,您可以尝试同时使用以下内容:

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

回答by Nayanesh Gupte

eg. you can use

例如。您可以使用

android:maxLength="13"

this will restrict texview length to 13 but problem is if you try to add 3 dots(...), it wont display it, as it will be part of textview length.

这会将 texview 长度限制为 13,但问题是如果您尝试添加 3 个点(...),它不会显示它,因为它将成为 textview 长度的一部分。

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

     } else {

            userName = data;

    }
        textView.setText(userName);

apart from this you have to use

除此之外,你必须使用

 android:maxLines="1"

回答by kyrylo_23

Use

  • android:singleLine="true"
  • android:maxLines="1"
  • app:layout_constrainedWidth="true"
  • android:singleLine="true"
  • android:maxLines="1"
  • app:layout_constrainedWidth="true"

It's how my full TextViewlooks:

这就是我的完整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" />

Picture of <code>TextView</code>

Picture of <code>TextView</code>

回答by Varad Mondkar

I am using Horizonal Recyclerview.
1) Here in CardView, TextView gets distorted vertically when using

我正在使用水平 Recyclerview。
1) 在 CardView 中,TextView 在使用时会垂直扭曲

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

Check the bold TextViews Wyman Group, Jaskolski... enter image description here

检查粗体 TextViews Wyman Group, Jaskolski ... enter image description here

2) But when I used singleLine along with ellipsize -

2)但是当我将 singleLine 与 ellipsize 一起使用时 -

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

Check the bold TextViews Wyman Group, Jaskolski... enter image description here

检查粗体 TextViews Wyman Group, Jaskolski ... enter image description here

2nd solution worked for me properly (using singleLine). Also I have tested in OS version: 4.1 and above (till 8.0), it's working fine without any crashes.

第二个解决方案对我很有效(使用 singleLine)。此外,我在操作系统版本:4.1 及更高版本(直到 8.0)中进行了测试,它运行良好,没有任何崩溃。

回答by Mayank Bhatnagar

You need to add following lines into your layout for the textview

您需要将以下几行添加到 textview 的布局中

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

Hope this works for you.

希望这对你有用。

回答by user1974433

I got the desired result by using

我通过使用得到了想要的结果

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

The trick is set maxLines and minLines to the same value... and Not just android:lines = "2", dosen't do the trick. Also you are avoiding any deprecated attributes.

诀窍是将 maxLines 和 minLines 设置为相同的值......而不仅仅是 android:lines = "2",不这样做。此外,您还避免了任何已弃用的属性。

回答by ruin3936

code:

代码:

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

xml:

xml:

android:maxLines = "5"

e.g.

例如

In Matthew 13, the disciples asked Jesus why He spoke to the crowds in parables. He answered, "It has been given to you to know the mysteries of the kingdom of heaven, but to them it has not been given.

在马太福音 13 章,门徒问耶稣为什么用比喻对众人说话。他回答说:“天国的奥秘,只叫你们知道,不叫他们知道。

Output: In Matthew 13, the disciples asked Jesus why He spoke to the crowds in parables. He answered, "It has been given to you to know...

输出:在马太福音 13 章,门徒问耶稣为什么用比喻对群众说话。他回答说:“你已经知道了......