Android 安卓可扩展文本视图,带有;查看更多“;按钮显示在3行后的中心

Android 安卓可扩展文本视图,带有;查看更多“;按钮显示在3行后的中心,android,textview,Android,Textview,我想在文本视图中显示说明,如果说明超过3行,我想显示一个view more按钮,该按钮可展开包含完整说明的文本视图(或包含完整说明的对话框) 我已经参考了一些链接 (一) 上面的解决方案在文本视图的第三行末尾有一个箭头状的图像视图,但如果描述跨越3行,我希望在文本视图下方显示一个按钮 (二) 上述解决方案未对齐“查看更多”(“查看”在第3行,而“更多”在第4行) 我在找这样的东西。 这就是我实现所需输出的方式 我要展开的MytextView是:tvDescription 我有一个名为BtnSem

我想在文本视图中显示说明,如果说明超过3行,我想显示一个view more按钮,该按钮可展开包含完整说明的文本视图(或包含完整说明的对话框)

我已经参考了一些链接
(一)

上面的解决方案在文本视图的第三行末尾有一个箭头状的图像视图,但如果描述跨越3行,我希望在文本视图下方显示一个按钮

(二)

上述解决方案未对齐“查看更多”(“查看”在第3行,而“更多”在第4行)

我在找这样的东西。

这就是我实现所需输出的方式

我要展开的MytextView是:tvDescription 我有一个名为BtnSemore的“查看更多”按钮

为了检查textview是否有超过4行,我为它设置了一个侦听器,如下所示:

tvDescription.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    if(expandable) {
                        expandable = false;
                        if (tvDescription.getLineCount() > 4) {
                            btnSeeMore.setVisibility(View.VISIBLE);
                            ObjectAnimator animation = ObjectAnimator.ofInt(tvDescription, "maxLines", 4);
                            animation.setDuration(0).start();
                        }
                    }
                }
            });
我保留了一个布尔值来检查textview是否已经展开,以便在折叠它时不会出现任何问题

如果textview有四行以上,布尔标志将为true,按钮将可见,因此这是展开和折叠动画的代码

btnSeeMore.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                if (!expand) {
                    expand = true;
                    ObjectAnimator animation = ObjectAnimator.ofInt(tvDescription, "maxLines", 40);
                    animation.setDuration(100).start();
                    btnSeeMore.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.ic_collapse));
                } else {
                    expand = false;
                    ObjectAnimator animation = ObjectAnimator.ofInt(tvDescription, "maxLines", 4);
                    animation.setDuration(100).start();
                    btnSeeMore.setImageDrawable(ContextCompat.getDrawable(getActivity(),R.drawable.ic_expand));
                }

            }
        });

下面的评论提供更多信息

实现这一目标的简单方法是

sample.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/tvSongDes"
        android:layout_marginStart="@dimen/dimen16"
        android:layout_marginEnd="@dimen/dimen16"
        android:layout_marginTop="@dimen/dimen16"
        android:maxLines="3"
        android:text="Dummy dummy Dummy dummy Dummy dummy Dummy dummy Dummy dummy Dummy dummy Dummy dummy Dummy dummy Dummy dummy Dummy dummy Dummy dummy Dummy dummy Dummy dummy Dummy dummy Dummy dummy Dummy dummy Dummy dummy Dummy dummy Dummy dummy Dummy dummy Dummy dummy Dummy dummy Dummy dummy Dummy dummy Dummy dummy Dummy dummy Dummy dummy Dummy dummy Dummy dummy Dummy dummy "
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        />

    <Button
        android:id="@+id/btShowmore"
        android:layout_width="wrap_content"
        android:layout_marginStart="@dimen/dimen16"
        android:text="Showmore..."
        android:textAllCaps="false"
        android:textColor="@android:color/holo_blue_light"
        android:background="@android:color/transparent"
        android:layout_height="wrap_content" />
</LinearLayout>

希望它能对某人有所帮助。

最新的方法,使用
MotionLayout
作为父布局,不需要自定义视图,只能使用xmls文件

MotionLayout
”是
constraintlayout
的子类

步骤如下:-

  • 使用
    ConstraintLayout
    作为父布局创建整个布局

  • 将父布局转换为MotionLayout

  • 现在我们需要通过我们将要创建的motionscene文件创建动画 如motionscene.xml。它将包含
    文本视图
    和更多按钮的状态 在morebutton的click事件之前和之后。它还包含 过渡信息

    此步骤的代码为:-

    <?xml version="1.0" encoding="utf-8"?>
    <MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto">
    
     <ConstraintSet android:id="@+id/start">
        <Constraint android:id="@+id/disc_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/discription_title">
    
            <CustomAttribute app:attributeName="maxLines"
                app:customIntegerValue="3"/>
    
        </Constraint>
    
    
    </ConstraintSet>
    
    <ConstraintSet android:id="@+id/end">
        <Constraint android:id="@+id/disc_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/discription_title">
    
            <CustomAttribute app:attributeName="maxLines"
                app:customIntegerValue="100"/>
    
        </Constraint>
        <Constraint android:id="@+id/more_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/disc_text">
    
            <CustomAttribute app:attributeName="alpha"
                app:customFloatValue="0.0"/>
    
        </Constraint>
    
    </ConstraintSet>
    
    <Transition
        app:constraintSetEnd="@id/end"
        app:motionInterpolator="easeIn"
    
        app:constraintSetStart="@+id/start"
        app:duration="200">
    
        <OnClick app:clickAction="transitionToEnd"
            app:targetId="@+id/more_text"/>
    
    </Transition>
    </MotionScene>
    
    
    
  • 将此布局设置为我们创建的父级
    MotionLayout
    layoutDiscrition
    最初创建的

    享受动画吧


  • 你们得到答案了吗?是的,我能得到它,寻找答案?你们能把它作为答案贴出来吗?所以其他成员可以看到它并从中获得帮助不要忘记在onGlobalListener中调用“getViewTreeObserver().RemoveOnGlobalYoutListener(this);”,否则您可能会进入无限循环。
    <?xml version="1.0" encoding="utf-8"?>
    <MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto">
    
     <ConstraintSet android:id="@+id/start">
        <Constraint android:id="@+id/disc_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/discription_title">
    
            <CustomAttribute app:attributeName="maxLines"
                app:customIntegerValue="3"/>
    
        </Constraint>
    
    
    </ConstraintSet>
    
    <ConstraintSet android:id="@+id/end">
        <Constraint android:id="@+id/disc_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/discription_title">
    
            <CustomAttribute app:attributeName="maxLines"
                app:customIntegerValue="100"/>
    
        </Constraint>
        <Constraint android:id="@+id/more_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/disc_text">
    
            <CustomAttribute app:attributeName="alpha"
                app:customFloatValue="0.0"/>
    
        </Constraint>
    
    </ConstraintSet>
    
    <Transition
        app:constraintSetEnd="@id/end"
        app:motionInterpolator="easeIn"
    
        app:constraintSetStart="@+id/start"
        app:duration="200">
    
        <OnClick app:clickAction="transitionToEnd"
            app:targetId="@+id/more_text"/>
    
    </Transition>
    </MotionScene>