您的位置:首页 > 其它

非常简单实现可折叠textview并带有省略号

2016-11-29 13:40 225 查看
效果如图


之前看到的可折叠textview都是代码一大堆,用起来麻烦。我这个方案不用写自定义控件,简单粗暴,直接达成目的。

布局文件<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:text="为天地立心,为生民立命,为往圣继绝学,为万世开太平。哈哈哈哈,强势装一波。"
android:id="@+id/tv_cbf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="25dp"
android:ellipsize="end"
android:maxLines="1"
android:textColor="@color/hint" />

<ImageView
android:id="@+id/iv_cbf"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
android:background="@mipmap/down_arrow" />
</RelativeLayout>设置默认maxlines是一排,并将ellipsize设置为end,旁边的imageview是按钮,然后设置监听
if (tvCbf.getMaxLines()==1){
tvCbf.setMaxLines(10);
ivCbf.setBackgroundResource(R.mipmap.up_arrow);
}else{
tvCbf.setMaxLines(1);
ivCbf.setBackgroundResource(R.mipmap.down_arrow);
}如果当前的textview的maxlines是1,那么久设置一个比较的数,并将图片换一张,或者用动画旋转。这种绝对是最简单的实现方式,并且亲测有效。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息