android TextView多行文本(超过3行)使用ellipsize属性无效问题的解决方法
时间:2021-05-28 14:02:09
布局文件中的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);
}
}
});
标签:android,TextView,ellipsize
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java中将base64编码字符串转换为图片的代码
2023-07-14 12:23:51
RocketMQ producer同步发送单向发送源码解析
2022-11-20 01:55:55
spring boot动态切换数据源的实现
2022-04-20 14:17:03
SpringCloud:feign对象传参和普通传参及遇到的坑解决
2023-02-17 11:18:58
Java日常练习题,每天进步一点点(58)
2021-06-26 01:13:02
![](https://img.aspxhome.com/file/2023/3/67483_0s.png)
mybatis plus自动生成代码的示例代码
2022-03-01 10:00:25
Java基于ShardingSphere实现分库分表的实例详解
2022-04-20 18:43:37
![](https://img.aspxhome.com/file/2023/0/76270_0s.jpg)
c#使用反射调用类型成员示例
2023-02-27 08:54:43
mybatis-plus QueryWrapper 添加limit方式
2022-12-09 02:08:11
Java Springboot整合支付宝接口的教程详解
2023-11-06 19:41:49
![](https://img.aspxhome.com/file/2023/9/58749_0s.png)
Java代码实现微信页面滚动防露底(核心代码)
2023-11-10 13:47:08
JAVA中Collections工具类sort()排序方法
2021-10-22 04:56:29
C语言实现二叉树的基本操作
2023-02-28 12:31:52
![](https://img.aspxhome.com/file/2023/6/120786_0s.png)
Java5种遍历HashMap数据的写法
2022-09-27 15:43:27
java简单实现数组中的逆序对
2022-06-26 02:23:17
Android使用OkHttp进行重定向拦截处理的方法
2022-09-12 15:47:32
![](https://img.aspxhome.com/file/2023/7/128947_0s.jpg)
浅谈c++性能测试工具之计算时间复杂度
2023-07-09 12:40:05
![](https://img.aspxhome.com/file/2023/4/79524_0s.jpg)
java实现一次性压缩多个文件到zip中的方法示例
2021-11-16 07:24:47
C#日期格式字符串的相互转换操作实例分析
2021-09-01 10:06:56
java开发RocketMQ消息中间件原理基础详解
2023-10-01 21:04:49