重要的是:
1、TextView 和ImageButton 的id必须按照库要求的设置,分别设置为”@id/expandable_text” 和”@id/expand_collapse” ,只有这样才能生效。
2、当然,你也可以设置以下属性满足你项目的需要。
maxCollapsedLines 折叠的时候允许显示的最大行,默认为8
animDuration 折叠展开时动画所用的时间,默认300ms
animAlphaStart 动画执行的时候,渐变动画,背景虚化。默认为0.7f,设置为1,则不会有虚化。
expandDrawable 你自定义展开动画。
collapseDrawable 你自定义折叠动画。
三、小例子。
<com.ms.square.android.expandabletextview.ExpandableTextView
xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:expandableTextView=“http://schemas.android.com/apk/res-auto”
android:id="@+id/expand_text_view"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
expandableTextView:maxCollapsedLines=“4”
expandableTextView:animDuration=“200”>
<TextView
android:id="@id/expandable_text"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
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>
// sample code snippet to set the text content on the ExpandableTextView
ExpandableTextView expTv1 = (ExpandableTextView) rootView.findViewById(R.id.sample1)
.findViewById(R.id.expand_text_view);
// IMPORTANT - call setText on the ExpandableTextView to set the text content to display
expTv1.setText(getString(R.string.dummy_text1));
需要注意的是,设置文本内容时候我们用的是ExpandableTextView 而不是Textview。
看效果
展开前:
展开后
。