温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android自定义View系列之可伸缩的TextView

发布时间:2020-08-18 13:13:29 来源:网络 阅读:516 作者:ceshiceshibaba 栏目:移动开发

经常访问我博客的同学应该注意到了我的博客大部分是关于分析Android系统原理的,很少涉及应用层的知识,但是想要开发一个用户欢迎的App,没有一个炫酷的UI肯定是不行的,就好比一个人想成为武林高手仅仅修炼内功是不行的,必须内功和外功同时修炼才能成为真正的武林高手。很多人在开发Android的App过程中有这样的感觉,我不用学习Android系统原理照样可以进行Android开发,而且可以做出非常炫酷的效果,确实是这样的 ,因为对于一款App,它的大部分需求无非就是请求数据然后展示数据,至于一些炫酷的UI控件网上有很多开源的,所以我们不用懂Android系统的原理同样可以进行App开发,相信你自己也发现了,长期做这样的工作,你的技术是永远得不到提升的,当你使用别人开源的炫酷的控件时,你并不知道别人是怎么实现的,即使你有兴趣看别人的实现方法,如果你不懂Android系统原理,估计你也看不懂别人为什么要这么做。所以从今天起的半年时间,我会将学习重点放在应用层的开发,主要是自定义View和炫酷的动画的学习上。


该自定义View具备如下特性:

  1. 当显示的内容没有2行(可以通过属性配置)时,和普通的TextView一样

  2. 当显示的内容大于2行时,仅仅显示两行,并且没有显示完的内容使用省略号代替,并显示向下箭头表示还有内容没有显示全

  3. 当用户点击TextView时,内容全部展开,当用户再次点击时,TextView又变为压缩模式

下面我们开始代码实现,正如上面所说,此View支持自定义属性,所以我们先要定义attrs.xml文件,如何编写attrs.xml文件网上有很多教程,我这里就不在说了。

<?xml version="1.0" encoding="utf-8"?><resources>
    <declare-styleable name="ExpandTextView">
        <attr name="textcolor" format="color"></attr>
        <attr name="textsize" format="dimension"></attr>
        <attr name="icon" format="reference"></attr>
        <attr name="lines" format="integer"></attr>
        <attr name="text" format="string"></attr>
    </declare-styleable></resources>12345678910

这几个属性的意义如下:

  • textcolor : 字体颜色

http://www.gexing.com/tupian/t/%E5%8D%97%E9%95%BF%E5%8C%BA%E5%A4%A7%E5%9C%A3%E5%A8%B1%E4%B9%90QQ82933700

http://www.gexing.com/tupian/t/%E8%92%99%E8%87%AA%E5%8E%BF%E5%A4%A7%E5%9C%A3%E5%BD%A9%E7%A5%A8QQ82933700/good/

http://www.gexing.com/tupian/t/%E6%97%8C%E9%98%B3%E5%8C%BA%E5%A4%A7%E5%9C%A3%E6%80%BB%E4%BB%A3QQ82933700

http://www.gexing.com/tupian/t/%E6%B8%AF%E5%8F%A3%E5%8C%BA%E5%A4%A7%E5%9C%A3%E5%BD%A9%E7%A5%A8QQ82933700/good/

http://www.gexing.com/tupian/t/%E8%99%9E%E5%9F%8E%E5%8E%BF%E5%A4%A7%E5%9C%A3%E5%A8%B1%E4%B9%90QQ82933700

http://www.gexing.com/tupian/t/%E6%98%AD%E5%B9%B3%E5%8E%BF%E5%A4%A7%E5%9C%A3%E6%8B%9B%E5%95%86QQ82933700/good/

http://www.gexing.com/tupian/t/%E9%93%B6%E6%B5%B7%E5%8C%BA%E5%A4%A7%E5%9C%A3%E6%80%BB%E4%BB%A3QQ82933700

http://www.gexing.com/tupian/t/%E5%AE%89%E6%96%B0%E5%8E%BF%E5%A4%A7%E5%9C%A3%E6%80%BB%E4%BB%A3QQ82933700

http://www.gexing.com/tupian/t/%E7%BA%A2%E5%B1%B1%E5%8C%BA%E5%A4%A7%E5%9C%A3%E6%8B%9B%E5%95%86QQ82933700/good/

http://www.gexing.com/tupian/t/%E5%9B%BA%E9%98%B3%E5%8E%BF%E5%A4%A7%E5%9C%A3%E6%80%BB%E4%BB%A3QQ82933700

http://www.gexing.com/tupian/t/%E5%87%8C%E6%B2%B3%E5%8C%BA%E5%A4%A7%E5%9C%A3%E6%8B%9B%E5%95%86QQ82933700/good/

http://www.gexing.com/tupian/t/%E7%A7%80%E8%8B%B1%E5%8C%BA%E5%A4%A7%E5%9C%A3%E5%A8%B1%E4%B9%90QQ82933700

http://www.gexing.com/tupian/t/%E7%BA%A2%E5%B1%B1%E5%8C%BA%E5%A4%A7%E5%9C%A3%E6%8B%9B%E5%95%86QQ82933700/good/

http://www.gexing.com/tupian/t/%E8%92%99%E5%B1%B1%E5%8E%BF%E5%A4%A7%E5%9C%A3%E6%8B%9B%E5%95%86QQ82933700

http://www.gexing.com/tupian/t/%E6%9E%9E%E9%98%B3%E5%8E%BF%E5%A4%A7%E5%9C%A3%E5%A8%B1%E4%B9%90QQ82933700/good/

http://www.gexing.com/tupian/t/%E9%87%91%E6%B2%99%E5%8E%BF%E5%A4%A7%E5%9C%A3%E6%80%BB%E4%BB%A3QQ82933700

http://www.gexing.com/tupian/t/%E9%93%81%E8%A5%BF%E5%8C%BA%E5%A4%A7%E5%9C%A3%E6%80%BB%E4%BB%A3QQ82933700/good/

http://www.gexing.com/tupian/t/%E7%91%9E%E9%87%91%E5%B8%82%E5%A4%A7%E5%9C%A3%E6%80%BB%E4%BB%A3QQ82933700

http://www.gexing.com/tupian/t/%E5%AE%89%E5%8C%96%E5%8E%BF%E5%A4%A7%E5%9C%A3%E6%8B%9B%E5%95%86QQ82933700/good/

http://www.gexing.com/tupian/t/%E4%B9%BE%E5%AE%89%E5%8E%BF%E5%A4%A7%E5%9C%A3%E5%BD%A9%E7%A5%A8QQ82933700

http://www.gexing.com/tupian/t/%E6%B8%85%E6%B2%B3%E5%8C%BA%E5%A4%A7%E5%9C%A3%E6%8B%9B%E5%95%86QQ82933700/good/

http://www.gexing.com/tupian/t/%E4%B8%B0%E9%A1%BA%E5%8E%BF%E5%A4%A7%E5%9C%A3%E5%BD%A9%E7%A5%A8QQ82933700/good/

http://www.gexing.com/tupian/t/%E9%97%B8%E5%8C%97%E5%8C%BA%E5%A4%A7%E5%9C%A3%E6%8B%9B%E5%95%86QQ82933700

http://www.gexing.com/tupian/t/%E4%B8%9C%E5%85%B4%E5%B8%82%E5%A4%A7%E5%9C%A3%E6%80%BB%E4%BB%A3QQ82933700/good/

http://www.gexing.com/tupian/t/%E9%A2%8D%E4%B8%8A%E5%8E%BF%E5%A4%A7%E5%9C%A3%E6%8B%9B%E5%95%86QQ82933700

http://www.gexing.com/tupian/t/%E9%87%91%E6%B2%99%E5%8E%BF%E5%A4%A7%E5%9C%A3%E5%A8%B1%E4%B9%90QQ82933700

http://www.gexing.com/tupian/t/%E6%B2%85%E9%99%B5%E5%8E%BF%E5%A4%A7%E5%9C%A3%E5%BD%A9%E7%A5%A8QQ82933700/good/

http://www.gexing.com/tupian/t/%E9%83%8A%E3%80%80%E5%8C%BA%E5%A4%A7%E5%9C%A3%E5%BD%A9%E7%A5%A8QQ82933700

http://www.gexing.com/tupian/t/%E5%8D%AB%E4%B8%9C%E5%8C%BA%E5%A4%A7%E5%9C%A3%E5%B9%B3%E5%8F%B0QQ82933700/good/

http://www.gexing.com/tupian/t/%E6%96%B0%E9%82%B1%E5%8C%BA%E5%A4%A7%E5%9C%A3%E5%B9%B3%E5%8F%B0QQ82933700

http://www.gexing.com/tupian/t/%E5%BC%A0%E5%BA%97%E5%8C%BA%E5%A4%A7%E5%9C%A3%E6%80%BB%E4%BB%A3QQ82933700/good/

http://www.gexing.com/tupian/t/%E5%AE%81%E6%98%8E%E5%8E%BF%E5%A4%A7%E5%9C%A3%E5%A8%B1%E4%B9%90QQ82933700

http://www.gexing.com/tupian/t/%E5%8F%8B%E8%B0%8A%E5%8E%BF%E5%A4%A7%E5%9C%A3%E5%B9%B3%E5%8F%B0QQ82933700

http://www.gexing.com/tupian/t/%E7%91%9E%E5%AE%89%E5%B8%82%E5%A4%A7%E5%9C%A3%E5%B9%B3%E5%8F%B0QQ82933700

http://www.gexing.com/tupian/t/%E6%B8%85%E5%9F%8E%E5%8C%BA%E5%A4%A7%E5%9C%A3%E6%80%BB%E4%BB%A3QQ82933700/good/

http://www.gexing.com/tupian/t/%E5%AE%81%E6%B5%B7%E5%8E%BF%E5%A4%A7%E5%9C%A3%E5%B9%B3%E5%8F%B0QQ82933700

http://www.gexing.com/tupian/t/%E5%9B%9B%E5%B9%B3%E5%B8%82%E5%A4%A7%E5%9C%A3%E5%BD%A9%E7%A5%A8QQ82933700/good/

http://www.gexing.com/tupian/t/%E7%9B%98%E5%B1%B1%E5%8E%BF%E5%A4%A7%E5%9C%A3%E6%8B%9B%E5%95%86QQ82933700

http://www.gexing.com/tupian/t/%E4%B8%9C%E5%AE%81%E5%8E%BF%E5%A4%A7%E5%9C%A3%E5%B9%B3%E5%8F%B0QQ82933700

http://www.gexing.com/tupian/t/%E6%B2%99%E6%B4%8B%E5%8E%BF%E5%A4%A7%E5%9C%A3%E6%8B%9B%E5%95%86QQ82933700/good/

http://www.gexing.com/tupian/t/%E5%AE%9C%E5%B7%9D%E5%8E%BF%E5%A4%A7%E5%9C%A3%E5%B9%B3%E5%8F%B0QQ82933700

http://www.gexing.com/tupian/t/%E5%AF%9F%E9%9B%85%E5%8E%BF%E5%A4%A7%E5%9C%A3%E5%B9%B3%E5%8F%B0QQ82933700/good/

http://www.gexing.com/tupian/t/%E5%91%A8%E5%AE%81%E5%8E%BF%E5%A4%A7%E5%9C%A3%E5%B9%B3%E5%8F%B0QQ82933700

http://www.gexing.com/tupian/t/%E5%90%88%E5%B1%B1%E5%B8%82%E5%A4%A7%E5%9C%A3%E5%B9%B3%E5%8F%B0QQ82933700/good/

http://www.gexing.com/tupian/t/%E8%B5%84%E9%98%B3%E5%8C%BA%E5%A4%A7%E5%9C%A3%E6%8B%9B%E5%95%86QQ82933700/good/

http://www.gexing.com/tupian/t/%E5%AE%9C%E7%AB%A0%E5%8E%BF%E5%A4%A7%E5%9C%A3%E5%B9%B3%E5%8F%B0QQ82933700

http://www.gexing.com/tupian/t/%E7%8F%A0%E6%99%96%E5%8C%BA%E5%A4%A7%E5%9C%A3%E6%8B%9B%E5%95%86QQ82933700/good/

http://www.gexing.com/tupian/t/%E7%BA%A2%E5%8E%9F%E5%8E%BF%E5%A4%A7%E5%9C%A3%E5%A8%B1%E4%B9%90QQ82933700

http://www.gexing.com/tupian/t/%E6%99%AE%E5%85%B0%E5%8E%BF%E5%A4%A7%E5%9C%A3%E6%8B%9B%E5%95%86QQ82933700/good/

http://www.gexing.com/tupian/t/%E8%BE%BD%E6%BA%90%E5%B8%82%E5%A4%A7%E5%9C%A3%E5%BD%A9%E7%A5%A8QQ82933700

http://www.gexing.com/tupian/t/%E5%B9%B3%E6%9E%9C%E5%8E%BF%E5%A4%A7%E5%9C%A3%E6%80%BB%E4%BB%A3QQ82933700/good/

http://www.gexing.com/tupian/t/%E8%B5%84%E5%85%B4%E5%B8%82%E5%A4%A7%E5%9C%A3%E5%B9%B3%E5%8F%B0QQ82933700

http://www.gexing.com/tupian/t/%E6%A6%86%E4%B8%AD%E5%8E%BF%E5%A4%A7%E5%9C%A3%E5%B9%B3%E5%8F%B0QQ82933700/good/

http://www.gexing.com/tupian/t/%E6%B5%B7%E5%8E%9F%E5%8E%BF%E5%A4%A7%E5%9C%A3%E6%80%BB%E4%BB%A3QQ82933700/good/

http://www.gexing.com/tupian/t/%E7%83%9F%E5%8F%B0%E5%B8%82%E5%A4%A7%E5%9C%A3%E5%A8%B1%E4%B9%90QQ82933700/good/

http://www.gexing.com/tupian/t/%E8%82%A5%E5%9F%8E%E5%B8%82%E5%A4%A7%E5%9C%A3%E5%BD%A9%E7%A5%A8QQ82933700

http://www.gexing.com/tupian/t/%E4%B8%B9%E5%87%A4%E5%8E%BF%E5%A4%A7%E5%9C%A3%E5%B9%B3%E5%8F%B0QQ82933700/good/

http://www.gexing.com/tupian/t/%E6%B5%B7%E6%B8%AF%E5%8C%BA%E5%A4%A7%E5%9C%A3%E5%A8%B1%E4%B9%90QQ82933700

http://www.gexing.com/tupian/t/%E5%8C%97%E6%B5%B7%E5%B8%82%E5%A4%A7%E5%9C%A3%E5%BD%A9%E7%A5%A8QQ82933700/good/

http://www.gexing.com/tupian/t/%E7%A7%80%E6%B4%B2%E5%8C%BA%E5%A4%A7%E5%9C%A3%E5%A8%B1%E4%B9%90QQ82933700

http://www.gexing.com/tupian/t/%E8%A1%A2%E6%B1%9F%E5%8C%BA%E5%A4%A7%E5%9C%A3%E5%A8%B1%E4%B9%90QQ82933700

http://www.gexing.com/tupian/t/%E8%A1%A2%E6%B1%9F%E5%8C%BA%E5%A4%A7%E5%9C%A3%E6%80%BB%E4%BB%A3QQ82933700/good/

http://www.gexing.com/tupian/t/%E4%B8%9C%E5%B1%B1%E5%8C%BA%E5%A4%A7%E5%9C%A3%E5%BD%A9%E7%A5%A8QQ82933700

http://www.gexing.com/tupian/t/%E4%BC%9A%E5%90%8C%E5%8E%BF%E5%A4%A7%E5%9C%A3%E5%B9%B3%E5%8F%B0QQ82933700/good/

http://www.gexing.com/tupian/t/%E5%87%8C%E6%B2%B3%E5%8C%BA%E5%A4%A7%E5%9C%A3%E5%A8%B1%E4%B9%90QQ82933700

http://www.gexing.com/tupian/t/%E4%BF%AE%E6%AD%A6%E5%8E%BF%E5%A4%A7%E5%9C%A3%E6%80%BB%E4%BB%A3QQ82933700

http://www.gexing.com/tupian/t/%E5%90%89%E6%9E%97%E5%B8%82%E5%A4%A7%E5%9C%A3%E5%A8%B1%E4%B9%90QQ82933700/good/

http://www.gexing.com/tupian/t/%E7%BD%97%E6%B1%9F%E5%8E%BF%E5%A4%A7%E5%9C%A3%E5%BD%A9%E7%A5%A8QQ82933700/good/

http://www.gexing.com/tupian/t/%E5%A3%A4%E5%A1%98%E5%8E%BF%E5%A4%A7%E5%9C%A3%E5%B9%B3%E5%8F%B0QQ82933700

http://www.gexing.com/tupian/t/%E4%BA%91%E9%BE%99%E5%8C%BA%E5%A4%A7%E5%9C%A3%E5%BD%A9%E7%A5%A8QQ82933700/good/

http://www.gexing.com/tupian/t/%E6%A1%91%E6%A4%8D%E5%8E%BF%E5%A4%A7%E5%9C%A3%E6%8B%9B%E5%95%86QQ82933700

http://www.gexing.com/tupian/t/%E9%A9%AC%E6%9D%91%E5%8C%BA%E5%A4%A7%E5%9C%A3%E6%8B%9B%E5%95%86QQ82933700/good/

http://www.gexing.com/tupian/t/%E4%B8%81%E9%9D%92%E5%8E%BF%E5%A4%A7%E5%9C%A3%E5%BD%A9%E7%A5%A8QQ82933700/good/

http://www.gexing.com/tupian/t/%E6%9F%8F%E4%B9%A1%E5%8E%BF%E5%A4%A7%E5%9C%A3%E5%BD%A9%E7%A5%A8QQ82933700

http://www.gexing.com/tupian/t/%E9%BE%99%E6%B2%99%E5%8C%BA%E5%A4%A7%E5%9C%A3%E6%80%BB%E4%BB%A3QQ82933700/good/

http://www.gexing.com/tupian/t/%E5%B9%B3%E6%9E%9C%E5%8E%BF%E5%A4%A7%E5%9C%A3%E5%B9%B3%E5%8F%B0QQ82933700/good/

http://www.gexing.com/tupian/t/%E5%A8%81%E4%BF%A1%E5%8E%BF%E5%A4%A7%E5%9C%A3%E5%B9%B3%E5%8F%B0QQ82933700

http://www.gexing.com/tupian/t/%E8%82%A5%E8%A5%BF%E5%8E%BF%E5%A4%A7%E5%9C%A3%E6%8B%9B%E5%95%86QQ82933700/good/

http://www.gexing.com/tupian/t/%E8%B4%B5%E5%8D%97%E5%8E%BF%E5%A4%A7%E5%9C%A3%E5%A8%B1%E4%B9%90QQ82933700/good/

http://www.gexing.com/tupian/t/%E6%A1%90%E6%A2%93%E5%8E%BF%E5%A4%A7%E5%9C%A3%E5%BD%A9%E7%A5%A8QQ82933700

http://www.gexing.com/tupian/t/%E5%BC%80%E6%B1%9F%E5%8E%BF%E5%A4%A7%E5%9C%A3%E6%8B%9B%E5%95%86QQ82933700

http://www.gexing.com/tupian/t/%E4%B9%90%E5%B9%B3%E5%B8%82%E5%A4%A7%E5%9C%A3%E6%8B%9B%E5%95%86QQ82933700/good/

http://www.gexing.com/tupian/t/%E5%AE%9C%E5%85%B4%E5%B8%82%E5%A4%A7%E5%9C%A3%E5%BD%A9%E7%A5%A8QQ82933700

http://www.gexing.com/tupian/t/%E6%B7%AE%E9%98%B4%E5%8C%BA%E5%A4%A7%E5%9C%A3%E5%B9%B3%E5%8F%B0QQ82933700

http://www.gexing.com/tupian/t/%E5%8F%8B%E5%A5%BD%E5%8C%BA%E5%A4%A7%E5%9C%A3%E5%BD%A9%E7%A5%A8QQ82933700/good/

http://www.gexing.com/tupian/t/%E6%9F%B3%E6%9E%97%E5%8E%BF%E5%A4%A7%E5%9C%A3%E5%A8%B1%E4%B9%90QQ82933700

http://www.gexing.com/tupian/t/%E5%AE%9D%E5%BA%94%E5%8E%BF%E5%A4%A7%E5%9C%A3%E6%8B%9B%E5%95%86QQ82933700/good/

http://www.gexing.com/tupian/t/%E7%AB%8B%E5%B1%B1%E5%8C%BA%E5%A4%A7%E5%9C%A3%E5%BD%A9%E7%A5%A8QQ82933700

http://www.gexing.com/tupian/t/%E8%9D%B6%E5%B1%B1%E5%8C%BA%E5%A4%A7%E5%9C%A3%E5%B9%B3%E5%8F%B0QQ82933700

http://www.gexing.com/tupian/t/%E8%8D%A3%E6%98%8C%E5%8E%BF%E5%A4%A7%E5%9C%A3%E5%B9%B3%E5%8F%B0QQ82933700/good/

http://www.gexing.com/tupian/t/%E4%B8%AD%E7%89%9F%E5%8E%BF%E5%A4%A7%E5%9C%A3%E6%80%BB%E4%BB%A3QQ82933700

http://www.gexing.com/tupian/t/%E9%AB%98%E9%9D%92%E5%8E%BF%E5%A4%A7%E5%9C%A3%E6%80%BB%E4%BB%A3QQ82933700/good/

http://www.gexing.com/tupian/t/%E8%B4%B5%E9%98%B3%E5%B8%82%E5%A4%A7%E5%9C%A3%E6%80%BB%E4%BB%A3QQ82933700

http://www.gexing.com/tupian/t/%E5%B0%A4%E6%BA%AA%E5%8E%BF%E5%A4%A7%E5%9C%A3%E5%A8%B1%E4%B9%90QQ82933700/good/

http://www.gexing.com/tupian/t/%E5%BC%A5%E5%8B%92%E5%8E%BF%E5%A4%A7%E5%9C%A3%E5%B9%B3%E5%8F%B0QQ82933700

http://www.gexing.com/tupian/t/%E7%8F%99%E3%80%80%E5%8E%BF%E5%A4%A7%E5%9C%A3%E5%B9%B3%E5%8F%B0QQ82933700/good/

http://www.gexing.com/tupian/t/%E5%AE%89%E5%B2%B3%E5%8E%BF%E5%A4%A7%E5%9C%A3%E5%B9%B3%E5%8F%B0QQ82933700/good/

http://www.gexing.com/tupian/t/%E7%BC%99%E4%BA%91%E5%8E%BF%E5%A4%A7%E5%9C%A3%E5%A8%B1%E4%B9%90QQ82933700/good/

http://www.gexing.com/tupian/t/%E9%B9%BF%E9%82%91%E5%8E%BF%E5%A4%A7%E5%9C%A3%E5%BD%A9%E7%A5%A8QQ82933700

http://www.gexing.com/tupian/t/%E6%B0%B8%E5%BE%B7%E5%8E%BF%E5%A4%A7%E5%9C%A3%E6%8B%9B%E5%95%86QQ82933700/good/

http://www.gexing.com/tupian/t/%E5%BC%A0%E6%B9%BE%E5%8C%BA%E5%A4%A7%E5%9C%A3%E6%8B%9B%E5%95%86QQ82933700



向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI