需求:
1、TextView加载文字,如果文字不多余两行,直接展示,“展开”按钮隐藏
2、如果文字大于2行,默认展示2行,在第二行,文字结尾处以 省略号 结尾,且在控件右下角出现“展开”按钮
3、如果文字展示的最后一行,文字结尾处距离控件终点距离较近,或完整展示N(N>2)行,则,“展开”按钮在下一行的终点处
效果图如下:
1、文字不大于2行
2、文字大于2行的默认展示
3、文字终点处,剩余空间足够展示 “展开”按钮
4、文字终点处,剩余空间不足以展示 “展开”按钮
实现:以下均为源码,直接复制即可使用
注:计算过程中,计算的数值,和布局中的值有关系,所以,请将下面代码,全部复制,跑起来后,布局和计算相结合的看。不然,有时候不明白这里为什么 减去15,那里为什么 减去20
attrs.xml文件中
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="ExpandTVStyle">
<!--最小行数-->
<attr name="minLineNum" format="integer"/>
<!--展开、收缩动画时间,单位:毫秒-->
<attr name="expandTime" format="integer"/>
<!--展开时对应的图片-->
<attr name="expandPicID" format="reference"/>
<!--不展开时对应的图片-->
<attr name="notExpandPicID" format="reference"/>
<!--展开时对应的文字-->
<attr name="expandTv" format="string"/>
<!--不展开时对应的文字-->
<attr name="notExpandTv" format="string"/>
<!--文字大小,单位:dp-->
<attr name="tvSize" format="integer"/>
</declare-styleable>
</resources>
view_expand_tv_layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ScrollView
android:id="@+id/expand_sv"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/expand_tv_root_rl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#55ff0000">
<TextView
android:id="@+id/expand_content_tv"
android:layout_width="match_parent"
android:layout_height="wrap_conten