可展开/收起的TextView

2019-04-21  本文已影响0人  jxiang112

在Android开发中,经常会用碰到这样的需求:描述信息比较长,默认情况下只显示几行,点击可以展开查看所有内容,再次点击有收起。
Android中没有这样的空间可以直接使用,所以本人对此进行封装开源了一个可展开/收起的TextView,具体可以查看:ExpandCollpaseTextView

ExpandCollpaseTextView是可以展开和收起的文本控件
添加依赖:
allprojects {
        repositories {
            ...
            maven { url 'https://jitpack.io' }
        }
    }
dependencies {
            implementation 'com.github.jxiang112:ExpandCollapseTextView:v1.0.2'
    }
特性:
原理:
属性:
属性名称 属性类型 属性默认值 属性描述
expand_text string 展开>> 展开按钮显示的文本
collapse_text string 收起>> 收起按钮显示的文本
expand_text_size integer 14 展开/折叠文本字体大小,单位sp
expand_text_color color #00C25F 展开/折叠文本颜色
content_text string 文本内容
text_line_height dimension 0 行高
content_text_size integer 14 文本文字大小
content_text_color color #333333 文本文字颜色
collapse_show_line_number integer 2 折叠状态下,显示的行数
expand_state enum collapse 设置展开折叠状态,collapse: 折叠状态;expend:展开状态
collapse_line_space_percent integer 20 折叠状态下,最后一行空白所占宽度的百分比,0-100
expend_click_event_on enum all 点击可以展开/收起的事件源,all:点击文本的任何地方都可以展开/收起;expand_text:只有点击展开/收起按钮才可以展开/收起
使用示例:
//xml布局文件中:
<com.wyx.components.widgets.ExpandCollpaseTextView
        android:id="@+id/expand_textview"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="12dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:content_text_color="#999999"
        app:content_text_size="12"
        app:text_line_height="2dp"
        app:expand_text_color="#06C362"
        app:collapse_line_space_percent="70"
        />
//java文件中:
ExpandCollpaseTextView expandTextView = findViewById(R.id.expand_textview);
expandTextView.setText("XXXXXXXXXXXXXXXXXXX");

效果预览:
image
上一篇 下一篇

猜你喜欢

热点阅读