Android TextView实现多文本折叠,展开效果

  

<强>背景

  

在开发过程中,当我们的需求中包含说说或者评论等内容的展示时,我们都会考虑当内容太多时该如何显示。当内容的字数太多,如果全部展示出来可能会影响体验效果,但是又不能只截取一部分内容进行展示,此时就需要考虑使用多行显示折叠的效果来实现。

  

<>强效果图:

  

 Android TextView实现多文本折叠,展开效果

  

<强>使用

  

1。布局文件调用

  

        LinearLayout & lt;   android: layout_width=" match_parent "   android: layout_height=" match_parent "   面向android:=按怪薄北?      & lt; com.wiggins.expandable.widget.MoreLineTextView   android: id=癅 + id/tv_more_line_short”   android: layout_width=" match_parent "   android: layout_height=" wrap_content "   android:背景=" @color/白”   android:填充=" @dimen/padding_small”   应用:clickAll=" true "   应用程序:输入textColor=癅color/红色/比;      & lt;视图/比;      & lt; com.wiggins.expandable.widget.expandable.ExpandableTextView   android: id=癅 + id/tv_expandable_short”   android: layout_width=" match_parent "   android: layout_height=" wrap_content "   android:背景=" @color/白”   android: ellipsize="结束"   android:填充=" @dimen/padding_small”   android:输入textColor=" @color/蓝”   应用:allClickable=" false "   应用:contentTextColor=" @color/蓝”   应用:isDisplayIcon=" false "   应用:maxCollapsedLines=4/比;      & lt;视图/比;      & lt; com.wiggins.expandable.widget.MoreLineTextView   android: id=癅 + id/tv_more_line_long”   android: layout_width=" match_parent "   android: layout_height=" wrap_content "   android:背景=" @color/白”   android:填充=" @dimen/padding_small”   应用:clickAll=" true "   应用程序:输入textColor=癅color/红色/比;      & lt;视图/比;      & lt; com.wiggins.expandable.widget.expandable.ExpandableTextView   android: id=癅 + id/tv_expandable_long”   android: layout_width=" match_parent "   android: layout_height=" wrap_content "   android:背景=" @color/白”   android: ellipsize="结束"   android:填充=" @dimen/padding_small”   android:输入textColor=" @color/蓝”   应用:allClickable=" false "   应用:contentTextColor=" @color/蓝”   应用:isDisplayIcon=" false "   应用:maxCollapsedLines=4/比;   & lt;/LinearLayout>      之前      

2. java文件调用

        私人空间initData () {   mTvMoreLineShort.setText (Constant.content1);   mTvExpandableShort.setText (Constant.content2);   mTvMoreLineLong.setText (Constant.content3);   mTvExpandableLong.setText (Constant.content4);   }   之前      

<强> MoreLineTextView使用

  

1。在attr.xml中定义属性

        & lt; declare-styleable name=" MoreTextStyle比;   & lt; !——内容大小——比;   & lt; attr=name=" textSize "格式"维度"/比;   & lt; !——内容颜色——比;   & lt; attr name==把丈?笆淙雝extColor”格式比;   & lt; !——内容默认最大行数——比;   & lt; attr name==罢?癿axLine”格式比;   & lt; !——展开/收起图标——比;   & lt; attr name==安慰肌?癳xpandIcon”格式比;   & lt; !——展开/收起动画执行时间——比;   & lt; attr name==罢?癲urationMillis”格式比;   & lt; !——可点击区域,默认展开/收起区域可点击——比;   & lt; attr name==安级?癱lickAll”格式比;   & lt;/declare-styleable>   之前      

2。是否显示折叠效果

  

        @Override   保护空白>/* *   * @Description设置显示内容   */公共空间setText (String str) {   mRelayout=true;   mTvContent.setText (str);   setVisibility (TextUtils.isEmpty (str) & # 63;视图。了:View.VISIBLE);   }   之前      

4。展开/收起动画

        @Override   公共空间>   & lt; declare-styleable name=" ExpandableTextView比;   & lt; !——内容默认最大行数,超过隐藏——比;   & lt; attr name==罢?癿axCollapsedLines”格式比;   & lt; !——展开/收起动画执行时间——比;   & lt; attr name==罢?癮nimDuration”格式比;   & lt; !——展开图片——比;   & lt; attr name==安慰肌?癳xpandDrawable”格式比;   & lt; !——收起图片——比;   & lt; attr name==安慰肌?癱ollapseDrawable”格式比;   & lt; !——内容颜色——比;   & lt; attr name==把丈?癱ontentTextColor”格式比;   & lt; !——内容大小——比;   & lt; attr=name=" contentTextSize "格式"维度"/比;   & lt; !——收起/展开颜色——比;   & lt; attr name==把丈?癱ollapseExpandTextColor”格式比;   & lt; !——收起/展开大小——比;   & lt; attr=name=" collapseExpandTextSize "格式"维度"/比;   & lt; !——收起文字——比;   & lt; attr name==白址?皌extCollapse”格式比;   & lt; !——展开文字——比;   & lt; attr name==白址?皌extExpand”格式比;   & lt; !——可点击区域,默认展开/收起区域可点击——比;   & lt; attr name==安级?癮llClickable”格式比;   & lt; !——是否显示展开/收起图标,默认显示——比;   & lt; attr name==安级?癷sDisplayIcon”格式比;   & lt; !——收起/展开位置,默认左边——比;   & lt; attr name=" collapseExpandGrarity比;   & lt;国旗名称==" https://www.yisu.com/zixun/3 "/白蟆奔壑当?   & lt;国旗名称==" https://www.yisu.com/zixun/5 "/罢贰钡募壑当?   & lt;/attr>   & lt; !——收起/展开图标位置,默认右边——比;   & lt; attr name=" drawableGrarity比;   & lt;国旗名称==" https://www.yisu.com/zixun/3 "/白蟆奔壑当?   & lt;国旗名称==" https://www.yisu.com/zixun/5 "/罢贰钡募壑当?   & lt;/attr>   & lt;/declare-styleable>   

Android TextView实现多文本折叠,展开效果