UI效果

Android-推荐一个折叠文本开源库(ExpandableTe

2022-07-25  本文已影响0人  阿博聊编程
图片来源网络,入侵必删

这篇博客继续分享一个折叠文本开源库——ExpandableTextView,希望能帮你提升大家的开发效率。

ExpandableTextView引入项目

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

截止我发博客,开源库版本是0.1.4,最新版本看开源库的文档

用法

1、XML里面:

  <!-- sample 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>

2、代码里面:

ExpandableTextView expTv1 = (ExpandableTextView) rootView.findViewById(R.id.sample1)
                    .findViewById(R.id.expand_text_view);
expTv1.setText(getString(R.string.dummy_text1));

API属性

属性 描述
maxCollapsedLines (默认为 8)TextView 折叠时允许显示的最大文本行数
animDuration (默认为 300 毫秒)展开/折叠动画的持续时间
animAlphaStart (默认为 0.7f) 动画开始时 TextView 的 Alpha 值(注意)如果要禁用 Alpha 动画,请将此值设置为 1
expandDrawable 自定义一个可绘制集到 ImageButton 以展开 TextView
collapseDrawable 自定义一个可绘制集到 ImageButton 以折叠 TextView

有一个需要注意的事情是:

TextView 和 ImageButton 的视图 ID 必须分别设置为“@id/expandable_text”和“@id/expand_collapse”才能使该库正常工作。

上一篇 下一篇

猜你喜欢

热点阅读