一个自定义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);