【问题标题】:How to show 3 dots at the end of text in textview如何在textview中的文本末尾显示3个点
【发布时间】:2014-02-17 09:57:05
【问题描述】:

我知道这个问题被问了很多次,我已经解决了所有的问题,但问题还是一样。

我希望我的文本长度为 50 个字符。现在它分为两行。

以下是我所做的:

 <TextView
            android:id="@+id/notification_Message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/notification_head"
            android:layout_centerHorizontal="true"
            android:layout_margin="10dp"
            android:gravity="center"
            android:ellipsize="end"
            android:maxLength="50"
            android:text=" Hello developer. please check this message for long length. max length to 50 characters. "   

            android:textSize="20sp" />

这里的文字只是我添加的示例文字。它没有在末尾显示 3 个点,而是显示最多 50 个字符的消息。我也试过 ma​​x lines,也试过 single line false。但没有积极的结果。 我也不想通过在逻辑上添加三个点来以编程方式处理这件事。

请帮忙

【问题讨论】:

    标签: android textview


    【解决方案1】:

    为您的 TextView 设置一个预设宽度并添加这些属性(向@T-D 致敬)

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

    maxLines 将确保您的文本不会以两行显示,而 ellipsize 将在最后添加三个点。

    旧答案

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

    【讨论】:

    • android:singleLine="true" 现在已弃用。请改用android:maxLines="1"
    • 我发现在android 4.4.2,它只显示一个点。是平时还是什么
    【解决方案2】:

    对于带有点的单行文本,但现在它已弃用,所以不要再使用singleLine

    感谢这篇文章,我发现如果我们有更多行,如何在文本末尾显示点,它对我有用:)

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

    在此处发布此答案,因为此处的所有答案都是针对单行而不是针对多行的

    【讨论】:

      【解决方案3】:

      android:ellipsize 仅适用于:

      android:singleLine="true"
      

      android:maxLines="#any number#"
      
      android:scrollHorizontally="true"
      

      因为 maxLength 会截断文本。

      因此,如果您只想显示 50 个字符 + 点,则必须使用代码管理标签,或者为 textview 留两行。

          <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:ellipsize="end"
          android:maxLines="2"
          android:scrollHorizontally="true"
          android:text="Hello developer. please check this message for long length. max length to 50 characters."
          android:textSize="20sp" />
      

      【讨论】:

      • 如果我希望最大字符数为 50 怎么办
      • Recap: - 文本视图必须独立显示 50 个字符,屏幕是纵向或横向的。 - 无论文本是 2 行还是 3 行 - 如果文本超过 50 个字符,则必须出现点 - 如果您使用 ellipsize,则 maxLanght 切点我看到的唯一方法是:String toCut = "Hello developer. please check this message for long length. max length to 50 characters."; if(toCut.length() &gt; 50){ toCut = toCut.substring(0, 50) + '…'; } textView.setText(toCut);
      • 我知道这是可以做到的。但后来我需要从这个 texview 中获取文本。然后它又会给我造成一团糟
      • 您可以使用 setTag(entireText) 和稍后的 getTag() 存储整个文本
      • 在我的情况下它不会滚动
      【解决方案4】:

      android:singleLine="true" 已弃用

      更好的使用

      android:maxLines="1"

      android:ellipsize="end" 在文本末尾添加 "..."

      【讨论】:

      • 知道如何在没有空格的情况下管理very_long_word中间的椭圆吗?它似乎在空格而不是 myVeryLongWord...
      【解决方案5】:

      使用singleLine = "true" 通过单行限制文本

      【讨论】:

      • 50 个字符不能在一行中表示
      • 问题是......singleLine 已被弃用,但我前段时间尝试过时,如果没有 singleLine = true ,我找不到使椭圆大小工作的方法。
      • 如果你需要精确显示50个字符,你应该在末尾附加3个点,并通过代码将限制更改为53
      • 您可以定期阅读它,我可以发誓我在文档中阅读过它,但那里不再说明。这是以前的文字:androidsbs.blogspot.co.at/2013/12/…
      • @PankajKumar 对,应该先检查文档。但是你知道这个关于singleLine 被弃用的谣言是从哪里来的吗?此外,据我所知,它在 IDE 中被标记为已弃用。
      【解决方案6】:

      我也为此苦苦挣扎。 android:maxLength 没有任何效果,所以我尝试了 android:maxWidth 并且它起作用了。

      在下面查看我的 sn-p。

      <TextView
              android:id="@+id/tv_comment_author"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:maxWidth="120dp"
              android:singleLine="true"
              android:ellipsize="end"
              android:text="@string/dummy_text"/>
      

      【讨论】:

        【解决方案7】:

        @Lokesh 的方法效果很好。

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

        但我意识到 TextView 会被单词截断(默认情况下)的问题。显示我们是否有如下文本:

        测试 long_line_without_any_space_abcdefgh

        TextView 将显示:

        测试...

        我找到了解决这个问题的解决方案,用 unicode 不间断空格字符替换空格,它使 TextView 在字符而不是单词上换行:

        yourString.replace(" ", "\u00A0");
        

        结果:

        测试 long_line_without_any_space_abc...

        【讨论】:

          【解决方案8】:

          创建您的自定义 TextView:

          <com.xyz.CustomizedTextViewName
                  android:id="@+id/app_title"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_gravity="center_vertical"
                  android:ellipsize="end"
                  android:singleLine="true"
                  android:maxEms="10"
          
                  app:typeface="roboto_bold"/>
          

          【讨论】:

            【解决方案9】:

            这就是你想要的答案:

                <TextView
                    android:id="@+id/tv_title"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="wrap_content"
                    android:text="Title"
                    android:maxLines="1"
                    android:ellipsize="end"
                    android:maxWidth="195dp"
                    android:textColor="@color/white"
                    android:textSize="14dp"
                    android:gravity="center"
                    />
            

            【讨论】:

              【解决方案10】:
              <TextView
                          android:id="@+id/txt__customizer_item"
                          android:layout_width="wrap_content"
                          android:layout_height="40dp"
                          android:gravity="center"
                          android:paddingRight="20dp"
                          android:singleLine="true"
                          android:text=" Hello developer. please check this message for long length. max length to 50 characters. "
                          android:textAppearance="@android:style/TextAppearance.Medium"
                          android:textColor="@color/light_blue"
                          android:textSize="20sp" />
              

              这对我有用。从右侧填充。它会自动显示...

              【讨论】:

              • 亲爱的,问题是 50 个字符不能在一行中出现。那么我们如何将单行设置为true
              • 那么你已经使用了 2 行。
              【解决方案11】:

              你可以通过xml

              <TextView 
                  android:id="@+id/textview"
                  android:maxLines="1" // or any number of lines you want
                  android:ellipsize="end" />
              

              【讨论】:

                【解决方案12】:

                如果singleLine="true"xml中被贬低,你仍然想使用单行行为,那么在代码中使用tv.setSingleLine(true);方法。

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2020-11-17
                  • 1970-01-01
                  • 2019-12-22
                  • 2018-12-11
                  • 1970-01-01
                  • 2019-10-24
                  • 2012-05-31
                  相关资源
                  最近更新 更多