写在前面
文本过长的展开和折叠,我本来的思路是通过TextView的 getLineCount() 方法判断文本内容的行数,然后这样进行展开或折叠。实际上,调用这个方法,在视图没有完全绘制时,getLineCount得到的行数永远是0。这个问题,可以使用观察者去监听来解决,下面是具体操作。
设置展示的最大行数
按照PM的要求,把最大行数设置为5行:
<!-- 内容 -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="start|center_vertical"
android:minHeight="30dp"
android:textColor="@color/menuFont"
android:textSize="16sp"
android:maxLines="5"
android:ellipsize="end" />
<!-- 展开或收起 -->
<TextView
android:layout_width