Android TextView Ellipsize (...) 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11261904/
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 06:34:37  来源:igfitidea点击:

TextView Ellipsize (...) not working

androidandroid-layoutandroid-linearlayouttextview

提问by Bins Ich

I want to have a single lined TextViewto show up 3 dots at the end when the text is longer than the TextView. I don't know why - but I don't get it.

我希望有一个单一的内衬TextView末现身3点,当文本比长TextView。我不知道为什么 - 但我不明白。

I already wrapped my head around similar StackOverflow questions, but I ended up with no solution. Maybe someone has some useful hints.

我已经在思考类似的 StackOverflow 问题,但最终没有解决方案。也许有人有一些有用的提示。

<LinearLayout 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent"
    android:orientation="vertical">

    <TextView 
        android:textStyle="bold" 
        android:text="Full Name" 
        android:layout_height="wrap_content" 
        android:textSize="16sp"
        android:layout_width="wrap_content" 
        android:id="@+id/lName"
        android:layout_gravity="center_vertical" 
        android:maxLines="1"
        android:ellipsize="end"/>
</LinearLayout>

The LinearLayoutabove is nested into 2 other LinearLayouts. Maybe this is important to know. I already tried the attribute "singleLine" too, but some say this is deprecated and it doesnt work anyway.

LinearLayout上述嵌套到2以外LinearLayouts。也许这很重要。我也已经尝试过属性“ singleLine”,但有人说这已被弃用,无论如何它都不起作用。

回答by Stefano Ortisi

Add the following styles in your styles file (typically styles.xml):

在您的样式文件中添加以下样式(通常为styles.xml):

<style name="autoscroll">
    <item name="android:singleLine">true</item>
    <item name="android:ellipsize">marquee</item>
    <item name="android:marqueeRepeatLimit">marquee_forever</item>
    <item name="android:focusable">true</item>
    <item name="android:focusableInTouchMode">true</item>
    <item name="android:scrollHorizontally">true</item>
</style>

Then add the style @style/autoscrollto your TextView:

然后将样式添加@style/autoscroll到您的TextView

<TextView android:id="@+id/lName"
      style="@style/autoscroll" />

You can reuse your autoscroll feature easily when you want this way.

当您需要这种方式时,您可以轻松地重复使用您的自动滚动功能。

回答by Khan

Add this in your xml for the TextView:

在 TextView 的 xml 中添加:

        android:maxWidth="200dp" 
        android:maxLines="1" 

As

作为

        android:singleLine="true"  

is deprecated.

已弃用。

回答by LuminiousAndroid

android:id="@+id/lName" android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:ellipsize="end"
    android:singleLine="true"
    android:text="Avinljhakjhsajkhakjshda"
    android:textSize="16sp"

回答by flawyte

It works with singleLine="true"but this attribute is now deprecated, use ellipsizeand scrollHorizontally="true"instead.

它的工作原理与singleLine="true"但这属性现在已经过时,使用ellipsizescrollHorizontally="true"代替。

回答by Zain

This doesn't work with me unless adding 'android:ems'attribute along with the android:ellipsizeattribute

这不跟我,除非将工作'android:ems'与沿属性android:ellipsize属性

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

回答by Adrian Clements

The ellipses did not appear while my text was selectable. I needed to disable selectable text (which is the default):

当我的文本可选时,省略号没有出现。我需要禁用可选文本(这是默认设置):

android:layout_width="0dp" (match constraint)
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:textIsSelectable="false"