当前位置: 首页 > news >正文

Android开发展开收起功能

Android开发展开收起功能

文字过长展开收起功能很普通,也很需要。

一、思路:

自定义控件MoreTextView

二、效果图:

在这里插入图片描述
在这里插入图片描述

三、关键代码:
public class MoreTextView2 extends LinearLayout {/*** TextView的实际高度*/private int textViewHeight;/*** 默认全文的Text*/private static final String EXPANDEDTEXT = "全文";/*** 默认收起的text*/private static final String COLLAPSEDTEXT = "收起";/*** 全文的text*/private String expandedText;/*** 收起的text*/private String collapsedText;/*** 字体大小*/private int textSize;/*** 字体颜色*/private int textColor;/*** 超过多少行出现全文、收起按钮*/private int trimLines;/*** 显示文本的TextView*/private TextView showTextView;/*** 全文和收起的TextView*/private TextView collapseTextView;/*** 是否是收起状态,默认收起*/private boolean collapsed = true;private boolean show;public MoreTextView2(Context context, AttributeSet attrs) {super(context, attrs);initView(context, attrs);}public MoreTextView2(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);initView(context, attrs);}@TargetApi(Build.VERSION_CODES.LOLLIPOP)public MoreTextView2(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {super(context, attrs, defStyleAttr, defStyleRes);initView(context, attrs);}private void initView(Context context, AttributeSet attrs) {showTextView = new TextView(context);setOrientation(VERTICAL);TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MoreTextView2);textColor = typedArray.getColor(R.styleable.MoreTextView2_textColor, Color.GRAY);textSize = typedArray.getDimensionPixelSize(R.styleable.MoreTextView2_textSize, 14);expandedText = typedArray.getString(R.styleable.MoreTextView2_expandedText);if (TextUtils.isEmpty(expandedText)) {expandedText = EXPANDEDTEXT;}collapsedText = typedArray.getString(R.styleable.MoreTextView2_collapsedText);if (TextUtils.isEmpty(collapsedText)) {collapsedText = COLLAPSEDTEXT;}trimLines = typedArray.getInt(R.styleable.MoreTextView2_trimLines, 0);typedArray.recycle();showTextView.setTextSize(textSize);showTextView.setTextColor(textColor);showTextView.setLineSpacing(3f, 1.2f);//hint(没有自我介绍内容时的默认显示)SpannableString ss = new SpannableString(getResources().getString(R.string.formal_tips));//定义hint的值AbsoluteSizeSpan ass = new AbsoluteSizeSpan(14, true);//设置字体大小 true表示单位是spss.setSpan(ass, 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);showTextView.setHint(new SpannedString(ss));showTextView.setHintTextColor(getResources().getColor(R.color.text_aaa));addView(showTextView);}public void setText(CharSequence text) {showTextView.setText(text);globalLayout();requestLayout();invalidate();sum();}
四、项目demo源码结构图:

在这里插入图片描述有问题或者需要完整源码的私信我

http://www.lryc.cn/news/460011.html

相关文章:

  • Sealos Devbox 发布,珍爱生命,远离 CI/CD
  • 数据结构——遍历二叉树
  • 【Ubuntu】在Ubuntu上安装IDEA
  • 解决:gpg: 从公钥服务器接收失败:服务器故障
  • 支持向量机SVM
  • 斯坦福UE4 C++课学习补充25:AI感知组件
  • 大模型 memory 记忆 缓存的应用
  • perl 给特定文件加上特定内容
  • 全面解析网络性能监控系统与网络故障排除技巧,助力IT运维高效管理
  • Centos7 搭建单机elasticsearch
  • 【前端】Bootstrap:JavaScript 组件与插件
  • 部署 Open WebUI
  • HUAWEI_HCIA_实验指南_Lib2.1_交换机基础配置
  • 第4天:用户界面和布局补充材料——`activity_login.xml`解读
  • 《深入浅出LLM基础篇》(五):Propmt工程优化
  • 基于WebSocket实现简易即时通讯功能
  • 2012年国赛高教杯数学建模D题机器人避障问题解题全过程文档及程序
  • Linux驱动开发——设备树
  • spring boot 2.7整合Elasticsearch Java client + ingest attachment实现文档解析
  • 一、PyCharm 基本快捷键总结
  • Windows系统C盘爆满了,如何清理?
  • 【C++】踏上C++学习之旅(一):初识C++和命名空间
  • tensorflow入门案例手写数字识别人工智能界的helloworld项目落地1
  • 深度学习——线性神经网络(三、线性回归的简洁实现)
  • 本地部署 Milvus
  • Git基础-配置http链接的免密登录
  • 华为OD机试真题-编码能力提升-2024年OD统一考试(E卷)
  • 高被引算法GOA优化VMD,结合Transformer-SVM的轴承诊断,保姆级教程!
  • 半小时速通RHCSA
  • 人工智能和机器学习之线性代数(一)