欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android TextView多行文本(超过3行)使用ellipsize属性无效问题的解决方法

程序员文章站 2023-11-22 08:05:52
布局文件中的textview属性复制代码 代码如下:

布局文件中的textview属性

复制代码 代码如下:

<textview
android:id="@+id/businesscardsingle_content_abstract"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margintop="5dp"
android:linespacingmultiplier="1.0"
android:lines="6"
android:text="@string/agrinbusiness_content"
android:textcolor="#7f7f7f"
android:textsize="13sp" />

 

 在java代码中控制文本的显示行数

复制代码 代码如下:

viewtreeobserver observer = textabstract.getviewtreeobserver(); //textabstract为textview控件
observer.addongloballayoutlistener(new ongloballayoutlistener() {

@override
public void ongloballayout() {
viewtreeobserver obs = textabstract.getviewtreeobserver();
obs.removeglobalonlayoutlistener(this);
if(textabstract.getlinecount() > 6) //判断行数大于多少时改变
  {
    int lineendindex = textabstract.getlayout().getlineend(5); //设置第六行打省略号
    string text = textabstract.gettext().subsequence(0, lineendindex-3) +"...";
    textabstract.settext(text);
  }
  }
});