详解Android TextView属性ellipsize多行失效的解决思路
作者:阴月有晴_ 时间:2022-03-02 16:00:32
本文介绍了Android TextView属性ellipsize多行失效的解决思路,分享给大家,具体如下:
多余文字显示省略号的常规做法
android:ellipsize="end" //省略号显示在末尾
android:ellipsize="middle" //省略号显示在中间
但是设置android:maxLines="2" 以后,ellipsize的值end有效,middle无效,本方法解决middle无效的问题
/**
* 字符串显示到textView, textView maxLines=2
* 如果字符串太长显示不下,则用省略号代替
* 省略号的位置在第一行末尾
*
* @param textView 显示字符串的view
* @param str 要显示的字符串
* @param width 显示字符串的view的宽
* @return 处理后带省略号的字符串
*/
private String ellipsizeString(TextView textView, String str, int width) {
Paint paint = textView.getPaint();
//文字总宽小于2倍的view宽,说明小于2行,直接返回
if (paint.measureText(str) < 2 * width) {
return str;
}
//存储显示到view的每行文字
List<String> list = new ArrayList<>();
int len = 0;
int start, end = 0;
while (len < str.length()) {
len += end;
int count = paint.breakText(str, end, str.length(), true, width, null);
start = end;
end = end + count;
list.add(str.substring(start, end));
}
//第一行文字末尾三个字符替换成省略号
String line1 = list.get(0);
line1 = line1.substring(0, line1.length() - 3) + "...";
//最后一行半的文字从末尾向前截取一行文字
String endLine = list.get(list.size() - 1);
int endLineWidth = (int) paint.measureText(endLine);
String minorEndLine = list.get(list.size() - 2);
int minorCuteCount = paint.breakText(minorEndLine, 0, minorEndLine.length(), true, endLineWidth, null);
String line2 = minorEndLine.substring(minorCuteCount, minorEndLine.length()) + endLine;
return line1 + line2;
}
代码的核心方法
paint.measureText(str) //测量字符串的宽度
paint.breakText(str, end, str.length(), true, width, null); //计算指定宽度下显示的字符串数量
都是api的方法,详细见源码
需要完善的地方,android设备的不同字符显示的宽度不同,三个字母替比省略号三个点要宽,m字符比l字符占位要宽,所以上面的方法处理后的字符串并不能使省略号显示到第一行末尾,有需要的可以在替换省略号那行代码那里精确处理一下,思路可以用上面的两个核心方法做一系列判断。
来源:https://www.jianshu.com/p/7edcc935640e
标签:Android,TextView,ellipsize
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java多线程之同步工具类Exchanger
2022-07-05 03:50:54
详解使用Maven开发Web应用详细步骤
2023-08-06 14:18:20
![](https://img.aspxhome.com/file/2023/6/58026_0s.png)
Java8使用stream实现list中对象属性的合并(去重并求和)
2023-06-23 13:44:40
![](https://img.aspxhome.com/file/2023/9/62379_0s.png)
springmvc 分页查询的简单实现示例代码
2022-01-09 11:08:22
详解怎么用Java的super关键字
2021-10-06 08:11:14
![](https://img.aspxhome.com/file/2023/7/86807_0s.png)
详解使用JavaCV/OpenCV抓取并存储摄像头图像
2023-05-08 07:23:31
Java基于虹软实现人脸识别、人脸比对、活性检测等
2023-02-18 15:29:09
![](https://img.aspxhome.com/file/2023/2/72392_0s.jpg)
解决IDEA无法下载maven依赖的问题
2023-12-05 17:06:59
![](https://img.aspxhome.com/file/2023/3/88503_0s.png)
Java代理模式与动态代理之间的关系以及概念
2022-03-15 18:05:34
Maven生命周期和及插件原理用法详解
2021-09-17 09:47:19
![](https://img.aspxhome.com/file/2023/2/75442_0s.png)
C# 通过NI-VISA操作Tektronix TBS 2000B系列示波器的实现步骤
2023-11-02 01:44:38
![](https://img.aspxhome.com/file/2023/0/84460_0s.png)
SpringBoot 配置文件总结
2021-09-06 13:12:57
![](https://img.aspxhome.com/file/2023/3/59343_0s.webp)
Java使用新浪微博API通过账号密码方式登陆微博的实例
2023-09-23 05:35:38
Java 迪杰斯特拉算法实现查找最短距离的实现
2022-05-23 01:47:43
![](https://img.aspxhome.com/file/2023/6/86706_0s.png)
SpringBoot+Swagger-ui自动生成API文档
2023-06-19 07:38:34
![](https://img.aspxhome.com/file/2023/0/85470_0s.png)
Java读写文件,在文件中搜索内容,并输出含有该内容的所有行方式
2022-12-14 18:23:58
![](https://img.aspxhome.com/file/2023/7/94207_0s.jpg)
使用IntelliJ IDEA搭建SSM框架的图文教程
2022-06-14 00:56:19
![](https://img.aspxhome.com/file/2023/7/94747_0s.png)
Java实现Andriod带看括弧的计算器代码
2023-04-06 09:36:00
java实现一个简单的网络爬虫代码示例
2021-08-05 13:59:12
详解SpringBoot注解读取配置文件的方式
2023-08-05 02:51:16