【问题标题】:android:ellipsize="end" and android:maxEms not workingandroid:ellipsize="end" 和 android:maxEms 不工作
【发布时间】:2015-12-11 18:26:46
【问题描述】:

我加了

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

致我的TextView,目的是在末尾显示三个点,最大文本限制为 8,但它不起作用。它既不显示点也不显示任何文本限制。

【问题讨论】:

    标签: android android-layout textview


    【解决方案1】:

    你添加“三点效果”的两个属性都是正确的

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

    但只有当TextView 在屏幕上没有任何可用空间来显示文本时才会出现这种效果。

    要设置TextView 中的文本限制,您可以使用

    android:maxLength="8"
    

    但它不会添加三个点,据我所知,您必须手动添加。

    类似的东西

    String text = "A bit longer text";
    if (text.length() > 8) {
        text = text.substring(0, 8) + "...";
    }
    

    android:maxEms 与以前不同,您可以阅读更多信息

    What is meant by Ems? (Android TextView)

    【讨论】:

    • 我想在最后显示三个点..阅读这个..stackoverflow.com/questions/12686468/…
    • 为什么 android:ellipsize="end" 不起作用?它应该显示三个点以防文本长度过大?
    • 你想如何限制文本?如果你的宽度是 match_parent 并且你的文本比一行中的多,它通常会起作用。
    • 我想将文本限制为最多 8 个字符,如果超过 8 个字符,那么它应该显示三个点..
    • 试试android:maxLength="8"
    【解决方案2】:

    ellipsize 不适用于宽度 wrap_content

    您可以直接尝试以下示例进行测试。

    <TextView
        android:layout_width="30dp"
        android:text="jhakjshdkajdhkjashkjdhakjsdjas"
        android:maxLines="1"
        android:lines="1"
        android:singleLine="true"
        android:ellipsize="end"
        android:layout_height="wrap_content" />
    

    希望这能帮助您解决问题。

    【讨论】:

    • ellipsize 与 layout_width 设置为 wrap_content 一起工作,但它必须占用它在屏幕上分配的所有可用空间。
    • 这应该是最佳答案
    【解决方案3】:

    不知为何,我不小心放了

    android:inputType="textCapSentences"
    

    在我的 TextView 上阻止我的省略号显示

    【讨论】:

    • 是的,它对我有用。经过 4 小时的搜索..
    • 好抓人!我更改了与线路设置相关的每个属性,但没有一个起作用。根据您的回答删除了inputType 属性,该死的椭圆终于出现了!
    【解决方案4】:

    最佳使用方法

     <androidx.appcompat.widget.AppCompatTextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:lines="1"
            android:maxLines="1"
            android:minEms="1"
            android:singleLine="true" />
    

    【讨论】:

      【解决方案5】:

      设置 android:layout_width="0dp" 而不是 android:layout_width="wrap_content" 为我工作

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-16
        • 2015-05-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多