自定义ExpandableTextView

一个自定义TextView,实现展开与合并,文字大小切换,文字颜色切换,效果如下:
这里写图片描述

自定义一个View继承自LinearLayout,代码如下:

package com.yootoo.expandabletextviewdemo.widget;

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

import com.yootoo.expandabletextviewdemo.R;

/**
 * 可以扩展的TextView
 *
 * @author LiuZhenli on 2016/12/8 22:14 Email: 848808263@qq.com
 * @version 1.0.0
 */

public class ExpandableTextView extends LinearLayout {

    private TextView tv_contentText,tv_more;

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

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

    public ExpandableTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttrs(attrs);
        initView();
    }
private int showLines;
    private void initView(){
        setOrientation(LinearLayout.VERTICAL);//设置布局方向
        LayoutInflater.from(getContext()).inflate(R.layout.layout_magic_text,this);
        tv_contentText = (TextView)findViewById(R.id.tv_contentText);
        tv_more = (TextView)findViewById(R.id.tv_more);
        tv_more.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                String textString = tv_more.getText().toString().trim();
                if("全文".equals(textString)){
                    tv_contentText.setMaxLines(Integer.MAX_VALUE);
                    tv_more.setText("收起");
                }else{
                    tv_contentText.setMaxLines(showLines);
                    tv_more.setText("全文");
                }
            }
        });
    }
    public static final int DEFAULT_MAX_LINES = 3;
    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(CharSequence content) {
        tv_contentText.setText(content);
        tv_contentText.post(new Runnable() {
            @Override
            public void run() {
                int linCount = tv_contentText.getLineCount();
                if (linCount > showLines) {
                    tv_contentText.setMaxLines(showLines);
                    tv_more.setVisibility(View.VISIBLE);
                    tv_more.setText("全文");
                } else {
                    tv_more.setVisibility(View.GONE);
                }
            }
        });

    }

    public void setTextColor(int color){
        tv_contentText.setTextColor(color);
    }
    public void setTextSize(int size){
        tv_contentText.setTextSize(size);
    }
}

layout_magic_text.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.yootoo.expandabletextviewdemo.activity.MainActivity"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_contentText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
    <TextView
        android:background="#eeaaff"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:id="@+id/tv_more"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="更多" />
</LinearLayout>

dimens.xml


    <declare-styleable name="ExpandTextView">
        <attr name="textcolor" format="color"/>
        <attr name="textsize" format="dimension"/>
        <attr name="icon" format="reference"/>
        <attr name="lines" format="integer"/>
        <attr name="text" format="string"/>
        <attr name="showLines" format="integer"/>
    </declare-styleable>

使用方法:
布局中添加:

 <com.yootoo.expandabletextviewdemo.widget.ExpandableTextView
            android:id="@+id/tv_text_hello"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="@dimen/activity_vertical_margin"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin" />

使用:

ExpandableTextView textView = (ExpandableTextView) findViewById(R.id.tv_text_hello);
        textView.setText(str1);
        textView.setTextColor(getResources().getColor(R.color.red));
        textView.setTextSize(20);

源码下载地址: http://download.csdn.net/detail/liuzhenlee/9707573

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值