【问题标题】:android append '…' at the end textview _EDITandroid 在文本视图 _EDIT 末尾追加 '...'
【发布时间】:2011-04-30 14:16:42
【问题描述】:

我有一个 lsit 视图,我需要添加一些文本。 在适配器中,我需要在最后附加 ... 并且我使用了以下内容 当我给

android:ellipsize="end"

只显示2行 例如:
asdnfsdfdsf asdfsdfsdf
sdfsd sdfsdf sdfsd 广告...

当我给的时候

android:ellipsize="start"

它是:
asdnfsdfdsf asdfsdfsdfd
...sdfsd sdfsdf sdfsd 广告

我使用的完整代码:

<TextView  android:layout_width="wrap_content" android:background="#016A7D"

   android:layout_height="80dp" android:textColor="#000000"

    android:maxLines="4"          

    android:id="@+id/list_report_desciption" 

    android:ellipsize="end"

/> 我得到的输出是:

更智能的星球究竟会是什么
看起来像? IT 正在发生怎样的变化?一个...

实际上它包含超过 6 行
有什么我需要设置的,以便我需要 3 行
谢谢

【问题讨论】:

  • 可能 80dp 的布局高度不足以容纳 3 行文本。在那里尝试 200 dp 之类的东西,看看你是否得到 4 或 5 行,然后将其修剪到你想要的高度,以获得 3 行。不完全优雅,所以我不会将其发布为答案。

标签: android listview append


【解决方案1】:

根据此报告,文本省略号似乎有问题: http://code.google.com/p/android/issues/detail?id=10554

一种解决方法允许省略一行文本,方法是将android:singleLine 属性设置为 true。但是这个属性已经被弃用了。

【讨论】:

    【解决方案2】:

    没有什么对我有用:-(
    我只是固定了长度。将字符串剪成该长度并在其后附加“...”。
    现在它为我工作。

    【讨论】:

      【解决方案3】:

      我自己正在寻找一个简单的解决方案,我最终得到了类似的东西:

      private String getTextWithMoreIndicator(TextView textView, String text) {
      
          long maxVisibleChars = textView.getPaint().breakText(text, true, textView.getMeasuredWidth(), null);
      
          if(maxVisibleChars < text.length()) {
              text = text.substring(0, (int)maxVisibleChars -3) + "..."; 
          }
      
          return text;
      }
      

      【讨论】:

        【解决方案4】:

        您接受的解决方案无法在所有设备上正确扩展。

        原因:“a”的宽度小于“A”,因此,如果您根据某个大小(例如 15 个字母)截断字符串并通过代码添加“...” .您可能会看到意想不到的结果。

        现在来解决问题:-

        解决方案:

        解决方案 #1: 将以下 3 个属性添加到您的 TextView 中,您将获得所需的结果:)

        <TextView
            android:ellipsize="end"
            android:lines="1"
            android:scrollHorizontally="true" />
        

        解决方案 #2: 另一种解决方法可能是,您可以决定选取文本(将文本从右向左移动的精美动画)。为此,您需要 TextView xml 中的以下属性:-

        <TextView
            android:id="@+id/my_text_view"
            android:ellipsize="marquee"
            android:lines="1"
            android:scrollHorizontally="true"
            android:marqueeRepeatLimit="marquee_forever" />
        

        然后在您的代码中,您需要通过其 id 获取 TextView 并输入以下行:-

        TextView myTextView = (TextView) findViewById(R.id.my_text_view);
        myTextView.setSelected(true);
        

        #### 编辑:- ####

        我刚刚发现,要让我的解决方案在高于 2.3.x 的 android 版本中工作,我们需要在 TextView xml 中添加以下行:-

            android:singleLine="true"
        

        虽然它是一个不推荐使用的属性,但你必须添加它,否则选框或“...”将不起作用。

        希望这能回答问题:)

        【讨论】:

          猜你喜欢
          • 2011-04-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-03-21
          • 2023-03-02
          • 2013-10-26
          • 2015-08-12
          相关资源
          最近更新 更多