首页 > 移动平台 > 详细

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

时间:2014-12-27 10:03:46      阅读:448      评论:0      收藏:0      [点我收藏+]

布局文件:

<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);
  }
  }
});
 

 

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

原文:http://www.cnblogs.com/zhujiabin/p/4188012.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!