Android 文字显示实现全文与收起

导读:本篇文章讲解 Android 文字显示实现全文与收起,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

效果图:

Android 文字显示实现全文与收起Android 文字显示实现全文与收起

1.布局文件1 :showmore.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/contentText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=""
        android:textColor="@color/shaper_gray"
        android:textSize="14sp" />

    <TextView
        android:id="@+id/textState"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:text=""
        android:textColor="@color/n_buttonblue"
        android:textSize="14sp" />

</LinearLayout>

2.在values文件夹下创建attr.xml文件

<?xml version="1.0" encoding="utf-8"?>
<resources>  
  <declare-styleable name="ExpandTextView">
        <attr name="showLines" format="integer"/>
    </declare-styleable>
</resources>

3.自定义View:ExpandTextView

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;
import android.widget.TextView;


public class ExpandTextView extends LinearLayout {
    public static final int DEFAULT_MAX_LINES = 3;//最大的行数
    private TextView contentText;
    private TextView textState;

    private int showLines;

    private ExpandStatusListener expandStatusListener;
    private boolean isExpand;

    public ExpandTextView(Context context) {
        super(context);
        initView();
    }

    public ExpandTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initAttrs(attrs);
        initView();
    }

    public ExpandTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttrs(attrs);
        initView();
    }

    private void initView() {
        setOrientation(LinearLayout.VERTICAL);
        LayoutInflater.from(getContext()).inflate(R.layout.showmore, this);
        contentText = (TextView) findViewById(R.id.contentText);
        if (showLines > 0) {
            contentText.setMaxLines(showLines);
        }

        textState = (TextView) findViewById(R.id.textState);
        textState.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                String textStr = textState.getText().toString().trim();
                if ("全文".equals(textStr)) {
                    contentText.setMaxLines(Integer.MAX_VALUE);
                    textState.setText("收起");
                    setExpand(true);
                } else {
                    contentText.setMaxLines(showLines);
                    textState.setText("全文");
                    setExpand(false);
                }
                //通知外部状态已变更
                if (expandStatusListener != null) {
                    expandStatusListener.statusChange(isExpand());
                }
            }
        });
    }

    private void initAttrs(AttributeSet attrs) {
        TypedArray typedArray = getContext().getTheme().obtainStyledAttributes(attrs, R.styleable.ExpandTextView, 0, 0);
        try {
            showLines = typedArray.getInt(R.styleable.ExpandTextView_showLines, DEFAULT_MAX_LINES);
        } finally {
            typedArray.recycle();
        }
    }

    public void setText(final CharSequence content) {
        contentText.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                // 避免重复监听
                contentText.getViewTreeObserver().removeOnPreDrawListener(this);

                int linCount = contentText.getLineCount();
                if (linCount > showLines) {

                    if (isExpand) {
                        contentText.setMaxLines(Integer.MAX_VALUE);
                        textState.setText("收起");
                    } else {
                        contentText.setMaxLines(showLines);
                        textState.setText("全文");
                    }
                    textState.setVisibility(View.VISIBLE);
                } else {
                    textState.setVisibility(View.GONE);
                }
                return true;
            }
        });
        contentText.setText(content);
//        contentText.setMovementMethod(new Move CircleMovementMethod(getResources().getColor(R.color.name_selector_color)));
    }

    public void setExpand(boolean isExpand) {
        this.isExpand = isExpand;
    }

    public boolean isExpand() {
        return this.isExpand;
    }

    public void setExpandStatusListener(ExpandStatusListener listener) {
        this.expandStatusListener = listener;
    }

    public static interface ExpandStatusListener {

        void statusChange(boolean isExpand);
    }
}

4.在Activity的布局添加

<com.june.wt.utils.ExpandTextView
    android:id="@+id/expandView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

调用:

expandView.setText("烧烤粉红色客服号烧烤粉红色看回放康师傅康师傅")

最后是交流公众号,大家可以关注一下

Android 文字显示实现全文与收起

 

 

 

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/119175.html

(0)
seven_的头像seven_bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!