您的位置:首页 > 移动开发 > Android开发

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

2014-11-25 14:10 921 查看
这篇文章介绍了android TextView多行文本(超过3行)使用ellipsize属性无效问题的解决方法,有需要的朋友可以参考一下
布局文件中的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);
  }
  }
});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐