可伸缩的textview详解(推荐)
作者:jingxian 时间:2021-11-06 00:34:51
在Android原生的TextView的基础上,可收缩/扩展的TextView:PhilExpandableTextView。
实现原理:核心是控制TextView的max lines。在TextView的初始化阶段但尚未绘制出View的时候,使用ViewTreeObserver,监听onPreDraw事件,获取TextView正常显示需要显示的总行数,但只给TextView设置最大运行的行数(小于总行数),从而造成TextView的收缩摘要效果,当用户通过按钮或其他方式扩展时候,把TextView的最大行数设置为正常显示完全的行数+1(+1是保持余量,避免不足)。
public class MainActivity extends Activity {
private String str = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for (int i = 0; i < 200; i++) {
str = str + i + " ";
}
final ExpandableTextView etv = (ExpandableTextView) findViewById(R.id.etv);
etv.setText(str);
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
boolean b = etv.getExpandablestatus();
b = !b;
etv.setExpandable(b);
}
});
}
}
PhilExpandableTextView.java:
package com.ganchuanpu.ExpandableTextView;
import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnPreDrawListener;
import android.widget.TextView;
import android.widget.Toast;
public class ExpandableTextView extends TextView {
// 最大行,默认显示3行
private final int MAX = 3;
// 完全展开需要的行数
private int lines;
private ExpandableTextView mExpandableTextView;
private boolean expandablestatus = false;
// 构造方法用两个参数的
public ExpandableTextView(Context context, AttributeSet attrs) {
super(context, attrs);
mExpandableTextView = this;
init();
}
private void init() {
// 在view绘制之前的时候执行,在onDraw之前
ViewTreeObserver mViewTreeObserver = this.getViewTreeObserver();
mViewTreeObserver.addOnPreDrawListener(new OnPreDrawListener() {
@Override
public boolean onPreDraw() {
// 避免重复监听
mExpandableTextView.getViewTreeObserver().removeOnPreDrawListener(this);
// 获得内容行数
lines = getLineCount();
return true;
}
});
setExpandable(false);
}
// 是否展开或者收缩,
// true,展开;
// false,不展开
public void setExpandable(boolean isExpand) {
if (isExpand) {
setMaxLines(lines + 1);
} else
setMaxLines(MAX);
expandablestatus = isExpand;
}
public boolean getExpandablestatus() {
return expandablestatus;
}
}
标签:textview,伸缩
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
解决Springboot项目启动后自动创建多表关联的数据库与表的方案
2023-11-24 01:11:27
![](https://img.aspxhome.com/file/2023/9/59719_0s.jpg)
配合Swagger使用绝佳的两款直观易用JSON可视化工具
2021-10-14 23:50:23
![](https://img.aspxhome.com/file/2023/2/88152_0s.png)
IDEA2020.1使用LeetCode插件运行并调试本地样例的方法详解
2022-02-28 09:44:47
![](https://img.aspxhome.com/file/2023/0/81190_0s.png)
Java面向对象程序设计:抽象类,接口用法实例分析
2023-03-08 23:56:07
![](https://img.aspxhome.com/file/2023/0/80870_0s.png)
详解Java8如何使用Lambda表达式进行比较
2023-12-09 19:27:16
java实现分布式项目搭建的方法
2022-10-13 10:42:36
![](https://img.aspxhome.com/file/2023/2/96262_0s.png)
C#数据结构之双向链表(DbLinkList)实例详解
2023-08-23 08:56:44
![](https://img.aspxhome.com/file/2023/4/96384_0s.png)
浅析SpringBoot2底层注解@Conditional@ImportResource
2023-08-01 23:35:51
![](https://img.aspxhome.com/file/2023/3/61793_0s.png)
解决RestTemplate加@Autowired注入不了的问题
2022-07-14 03:00:48
![](https://img.aspxhome.com/file/2023/0/96830_0s.png)
C#画笔Pen绘制光滑模式曲线的方法
2021-11-26 05:13:33
基于C#实现乱码视频效果
2022-01-25 14:29:56
![](https://img.aspxhome.com/file/2023/8/98218_0s.png)
Flutter之Timer实现短信验证码获取60s倒计时功能的代码
2023-07-20 20:00:30
![](https://img.aspxhome.com/file/2023/8/89528_0s.gif)
C# SQLite库使用技巧
2023-10-27 19:05:21
![](https://img.aspxhome.com/file/2023/2/75602_0s.jpg)
Android 文件存储系统原理
2021-12-24 05:10:57
SpringBoot实现过滤器拦截器的耗时对比
2022-04-12 00:45:04
![](https://img.aspxhome.com/file/2023/6/83456_0s.png)
同时使用@LoadBalanced @RefreshScope注解负载均衡失效分析
2023-12-07 10:59:24
![](https://img.aspxhome.com/file/2023/7/62037_0s.png)
详解java整合solr5.0之solrj的使用
2023-07-23 03:12:06
![](https://img.aspxhome.com/file/2023/3/57493_0s.png)
.NET/C#实现识别用户访问设备的方法
2021-12-20 06:30:58
java实现给出分数数组得到对应名次数组的方法
2021-06-01 13:58:05
java导出csv格式文件的方法
2022-07-24 20:26:36
![](https://img.aspxhome.com/file/2023/5/62665_0s.jpg)