开源项目ExpandableTextView使用和源码分析

ExpandableTextView,可展开和收起的TextView,就像GooglePlay里面显示应用的描述那样。

项目地址:
https://github.com/Manabu-GT/ExpandableTextView
其中包含项目源码和示例代码。

运行效果图:


在国内很多应用中,也可以看到这种效果的使用场景。如豌豆荚的详情页:


一、项目使用

(1).在工程的build.gradle文件中添加项目引用。

该项目较简单,java代码部分只有ExpandableTextView.java一个文件。

dependencies {
    compile 'com.ms-square:expandableTextView:0.1.4'
}
(2).在布局中添加如下代码。
注意,TextView的id只能定义为expandable_text,ImageButton的id只能定义为expand_collapse。
<com.ms.square.android.expandabletextview.ExpandableTextView
        android:id="@+id/expand_text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <TextView
        android:id="@id/expandable_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:textSize="16sp"
        android:textColor="#666666" />
    <ImageButton
        android:id="@id/expand_collapse"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="16dp"
        android:layout_gravity="right|bottom"
        android:background="@android:color/transparent"/>
</com.ms.square.android.expandabletextview.ExpandableTextView>
ExpandableTextView支持使用自定义属性。对应的attrs.xml文件及属性释义如下。
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="ExpandableTextView">
        <!-- 默认折叠状态下,文字最多显示的行数 -->
        <attr name="maxCollapsedLines" format="integer"/>
        <!-- 动画时长 -->
        <attr name="animDuration" format="integer"/>
        <!-- 动画启动时文字的透明度 -->
        <attr name="animAlphaStart" format="float"/>
        <!-- 将文字展开的图标(向下的箭头) -->
        <attr name="expandDrawable" format="reference"/>
        <!-- 将文字收起的图标(向上的箭头) -->
        <attr name="collapseDrawable" format="reference"/>
    </declare-styleable>
</resources>
(3).添加Java代码。
ExpandableTextView
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值