欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android TextView 文本展开收起

程序员文章站 2022-04-02 22:17:58
在界面上需要展示一个TextView,默认超过4行则在末尾添加"展开",用户点击展开文本后则完整显示文本,同时末尾添加“收起”。先列出TextView的布局

在界面上需要展示一个TextView,默认超过4行则在末尾添加"展开",用户点击展开文本后则完整显示文本,同时末尾添加“收起”。

先列出TextView的布局

 <TextView
     android:id="@+id/content"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_marginStart="20dp"
     android:layout_marginEnd="20dp"
     android:textColor="#666666"
     android:lineSpacingExtra="3dp"
     android:textSize="13sp"
     />

由于我们需要根据文本是否达到4行来做对应展示,很自然的想到要在TextView布局完成之后再处理,此处设置onPreDrawListener

contentTextView.getViewTreeObserver()
               .addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                   @Override
                   public boolean onPreDraw() {

                       contentTextView.getViewTreeObserver().removeOnPreDrawListener(this);

                       expandText();
                       return false;
                   }
               });

在expandText中,如果文本布局超过4行,我们需要截取第四行,在文本后面预留展开的位置,最后通过设置span添加点击事件,收起功能类似。

    String expand = "[展开]";
    String collapse = "[收起]";

    private void expandText() {

        CharSequence text = contentTextView.getText();

        int width = contentTextView.getWidth();
        TextPaint paint = contentTextView.getPaint();
        Layout layout = contentTextView.getLayout();
        int line = layout.getLineCount();
        if (line > 4) {
            int start = layout.getLineStart(3);
            int end = layout.getLineVisibleEnd(3);
            CharSequence lastLine = text.subSequence(start, end);

            float expandWidth = paint.measureText(expand);
            float remain = width - expandWidth;

            CharSequence ellipsize =
                    TextUtils.ellipsize(lastLine,
                                        paint,
                                        remain,
                                        TextUtils.TruncateAt.END);

            ClickableSpan clickableSpan = new ClickableSpan() {

                @Override
                public void onClick(@NonNull View widget) {

                    collapseText();
                }
            };

            SpannableStringBuilder ssb = new SpannableStringBuilder();
            ssb.append(text.subSequence(0, start));
            ssb.append(ellipsize);
            ssb.append(expand);
            ssb.setSpan(new ForegroundColorSpan(0xFF7369F8),
                        ssb.length() - expand.length(), ssb.length(),
                        Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            ssb.setSpan(clickableSpan,
                        ssb.length() - expand.length(), ssb.length(),
                        Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            contentTextView.setMovementMethod(LinkMovementMethod.getInstance());
            contentTextView.setText(ssb);
        }
    }

    private void collapseText() {

        // 默认此时文本肯定超过行数了,直接在最后拼接文本
        ClickableSpan clickableSpan = new ClickableSpan() {

            @Override
            public void onClick(@NonNull View widget) {

                expandText();
            }
        };

        SpannableStringBuilder ssb = new SpannableStringBuilder();
        ssb.append(bean.getMsg());
        ssb.append(collapse);
        ssb.setSpan(new ForegroundColorSpan(0xFF7369F8),
                    ssb.length() - collapse.length(), ssb.length(),
                    Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        ssb.setSpan(clickableSpan,
                    ssb.length() - collapse.length(), ssb.length(),
                    Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        contentTextView.setText(ssb);
    }

本文地址:https://blog.csdn.net/w_xue/article/details/108726566

相关标签: android