ExpandableTextView展开加动画的textview

经常看新闻或者视频介绍的话经常文字需要有展开功能,
如下在这里插入图片描述
像这种的话githu如果自己的写的话肯定就是一个button加两个textview或者一个textview动态设置行数(也可自定义view实现),但你也要处理动画问题。这里我找了个库:

https://github.com/Manabu-GT/ExpandableTextView

使用起来也是非常简单而且要求很低,只需要api>8即可
step 1:

dependencies {
    compile 'com.ms-square:expandableTextView:0.1.4'
}

step 2:
最关键的是textview必须设置id为expandable_text,
ImageButton 必须设置id为expand_collapse,有下面几个属性:
maxCollapsedLines (defaults to 8) :当文字展开的允许最大的行数
animDuration :动画持续时间

animAlphaStart (defaults to 0.7f) :渐变动画
expandDrawable :自定义打开的图片,就是点击imagrbutton的打开drawable

collapseDrawable :自定义折叠的图片,就是点击imagrbutton的收缩drawable
最后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>

// 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));
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值