博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
解决android TextView多行文本(超过3行)使用ellipsize属性无效问题
阅读量:7046 次
发布时间:2019-06-28

本文共 962 字,大约阅读时间需要 3 分钟。

布局文件中的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);
  }
  }
});

转载于:https://www.cnblogs.com/wubingshenyin/p/3310118.html

你可能感兴趣的文章
找出一个数组中唯一没有成对出现的两个数
查看>>
Ajax (部分一)自己做的,总结页面向后台传Form值、单个值和后台向前台传一个或是一组值...
查看>>
web缓存技术
查看>>
集合知识点
查看>>
Linux快捷键
查看>>
文档对象模型DOM
查看>>
2019北京国际康复及个人健康博览会将在中国国际展览中心举办
查看>>
JVM——类加载机制(一)
查看>>
超清晰的 DNS 原理入门指南 (资源)
查看>>
大神笔记
查看>>
spring cloud构建java b2b2c 电子商务云商平台
查看>>
阿里顶级Java架构师,教你这样手写Spring!
查看>>
android课程表控件、悬浮窗、Todo应用、MVP框架、Kotlin完整项目源码
查看>>
CLOB字段在java中操作
查看>>
磁盘清理
查看>>
javascript 判断数据类型 判空
查看>>
matplotlib 中文字体问题
查看>>
protobuf v3测试
查看>>
(1)知识准备【利用objective-c的runtime特性,结合FMDB实现一个轻量级的ORM】
查看>>
Angular js开发的各种坑(持续更新中。。。)
查看>>