ExpandableTextView,可展开和收起的TextView,就像GooglePlay里面显示应用的描述那样。
项目地址:
https://github.com/Manabu-GT/ExpandableTextView
其中包含项目源码和示例代码。
运行效果图:
注意,TextView的id只能定义为expandable_text,ImageButton的id只能定义为expand_collapse。
项目地址:
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