没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
Android TextView仿微信可折叠效果仿微信可折叠效果
主要为大家详细介绍了Android TextView仿微信可折叠效果,具有一定的参考价值,感兴趣的小伙伴们可以参考
一下
在微信朋友圈中,发送大量的文本信息时,在展示的时候微信会将该文本信息进行折叠处理,出现“全文”,“收起”的操作提
示。当点击全文时,才能看到全部的文本信息,正好最近的项目中也提出了类似的需求,这里就对该自定义View的实现的方
法进行了整理。
代码如下:
1.自定义的View:
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
* 可折叠的textview
*/
public class ExpandTextView extends LinearLayout {
public static final int DEFAULT_MAX_LINES = 3;
private TextView contentText;
private TextView textPlus;
private int showLines;
private ExpandStatusListener expandStatusListener;
private boolean isExpand;
public ExpandTextView(Context context) {
super(context);
initView();
}
public ExpandTextView(Context context, AttributeSet attrs) {
super(context, attrs);
initAttrs(attrs);
initView();
}
public ExpandTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initAttrs(attrs);
initView();
}
private void initAttrs(AttributeSet attrs) {
TypedArray typedArray = getContext().getTheme().obtainStyledAttributes(attrs, R.styleable.ExpandTextView, 0, 0);
try {
showLines = typedArray.getInt(R.styleable.ExpandTextView_showLines, DEFAULT_MAX_LINES);
}finally {
typedArray.recycle();
}
}
private void initView() {
setOrientation(LinearLayout.VERTICAL);
LayoutInflater.from(getContext()).inflate(R.layout.layout_magic_text, this);
contentText = (TextView) findViewById(R.id.contentText);
if(showLines > 0){
contentText.setMaxLines(showLines);
}
textPlus = (TextView) findViewById(R.id.textPlus);
textPlus.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
String textStr = textPlus.getText().toString().trim();
if("全文".equals(textStr)){
contentText.setMaxLines(Integer.MAX_VALUE);
textPlus.setText("收起");
setExpand(true);
}else{
contentText.setMaxLines(showLines);
资源评论
weixin_38656463
- 粉丝: 2
- 资源: 905
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功