使用
build.gradle添加
compile ‘com.ms-square:expandableTextView:0.1.4’
xml
<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>
java
ExpandableTextView expTv1 = (ExpandableTextView) findViewById(R.id.expand_text_view);
expTv1.setText(getString(R.string.dummy_text1));
效果
支持ListView
属性
maxCollapsedLines:(默认为8)显示的未被折叠的文本行数
animDuration:(默认为300ms)为展开/折叠的动画时间
animAlphaStart:(默认为0.7f)当动画开始时文字的透明的度。如果您想禁用透明度,设置这个值为1
expanddrawable:自定义ImageButton展开的图像设置
collapsedrawable:自定义ImageButton折叠的图像设置
样式修改
看源码得知不能设置水平布局
如果想做成如下效果,把剪头拿到右边:
可以这样布局
<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"
android:layout_below="@+id/item_distrib_goodsImage"
android:layout_marginTop="-15dp"
android:layout_toRightOf="@+id/item_distrib_goodsImage"
expandableTextView:animAlphaStart="1"
expandableTextView:animDuration="200"
expandableTextView:collapseDrawable="@drawable/expandable_textview_button_collapse"
expandableTextView:expandDrawable="@drawable/expandable_textview_button_expand"
expandableTextView:maxCollapsedLines="1"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@id/expandable_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="@color/colorThree"
android:textSize="@dimen/fontFour"
/>
<ImageButton
android:id="@id/expand_collapse"
android:layout_width="13dp"
android:layout_height="13dp"
android:background="@android:color/transparent"
android:padding="5dp"
/>
</LinearLayout>
</com.ms.square.android.expandabletextview.ExpandableTextView>
注意
需要注意的点为:
1、中间加一个LinearLayout,设置android:orientation=”horizontal”
2、LinearLayout中的TextView设置android:layout_weight=”1”
3、图中剪头展开和折叠的样式是通过设置expanddrawable和
collapsedrawable来实现的
4、我贴出的代码中
expandableTextView:collapseDrawable="@drawable/expandable_textview_button_collapse"
expandableTextView:expandDrawable="@drawable/expandable_textview_button_expand"
分别是两个drawable下的文件,源码为:
expandable_textview_button_expand.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<bitmap android:src="@mipmap/bg_arrow_right_gray" />
</item>
</layer-list>
expandable_textview_button_collapse.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<bitmap android:src="@mipmap/bg_arrow_down_gray" />
</item>
</layer-list>
5、TextView和ImageButton的id必须为”@id/expandable_text” 和 “@id/expand_collapse”
A ExpandableTextView BY Manabu-GT:
https://github.com/Manabu-GT/ExpandableTextView