在工作中有时候遇到这样的效果
源码如下:
package com.example.bkhu.myapplication;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
* Created by bkhu on 17/1/2.
*/
public class cutomeView extends LinearLayout {
private boolean isExprand = false;
private TextView contentView = null;
private TextView expandText = null;
private int mMaxCollapseLine = 3;
private Drawable expandDrable = null;
private Drawable collapseDrawable = null;
public cutomeView(Context context) {
this(context, null);
}
public cutomeView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public cutomeView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.cutomeView, defStyleAttr, 0);
expandDrable = a.getDrawable(R.styleable.cutomeView_expand_drawable);
collapseDrawable = a.getDrawable(R.styleable.cutomeView_collapse_drawable);
if (expandDrable != null) {
expandDrable.setBounds(0, 0, 48, 24);
}
if (collapseDrawable != null) {
collapseDrawable.setBounds(0, 0, 48, 24);
}
initView(context);
}
public void initView(Context context) {
if (getChildCount() > 0) {
removeAllViews();
}
TextView titleTextView = new TextView(context);
titleTextView.setText("title");
addView(titleTextView);
contentView = new TextView(context);
contentView.setEllipsize(TextUtils.TruncateAt.END);
contentView.setTextSize(15);
contentView.setText("内容内容内容内容内容内容内容内容内容" +
"内容内容内容内容内容内容内内容内容内容内容内容内容内容内容内容内容" +
"容内容内容内容内容内容内容内容内容内容内容内容内" +
"容内容内容内容内容内容内容内容内容内容内容内容内容" +
"内容内容内容内容内容内容内容内容内容内容内容内容内容内容" +
"内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容" +
"内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容");
addView(contentView);
expandText = new TextView(context);
expandText.setEllipsize(TextUtils.TruncateAt.END);
contentView.setTextSize(15);
expandText.setText("点击");
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.RIGHT;
expandText.setLayoutParams(params);
addView(expandText, params);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (expandText != null) {
expandText.setVisibility(View.GONE);
}
if (contentView != null) {
contentView.setMaxLines(Integer.MAX_VALUE);
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (contentView != null && !isExprand) {
contentView.setMaxLines(mMaxCollapseLine);
}
if (expandText != null) {
expandText.setVisibility(View.VISIBLE);
if (isExprand) {
expandText.setCompoundDrawables(null, null, collapseDrawable, null);
} else {
expandText.setCompoundDrawables(null, null, expandDrable, null);
}
expandText.setText(getText());
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
public void setExprand(boolean isexprand) {
isExprand = !isexprand;
requestLayout();
if (mOnChangeCallBack != null) {
mOnChangeCallBack.change(isExprand);
}
}
public boolean getExprand() {
return isExprand;
}
private String getText() {
return isExprand ? "收起" : "展开";
}
public OnChangeCallBack mOnChangeCallBack;
public void setOnChangeCallBack(OnChangeCallBack onChangeCallBack) {
this.mOnChangeCallBack = onChangeCallBack;
}
public interface OnChangeCallBack {
void change(boolean isExprand);
}
}
attrs.xml
<attr name="expand_drawable" format="reference"/>
<attr name="collapse_drawable" format="reference"/>
使用方式:
View view = inflater.inflate(R.layout.item_view, null);
final cutomeView cutomeView = (cutomeView) view.findViewById(R.id.cotume_view);
cutomeView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cutomeView.setExprand(isExprand);
}
});
cutomeView.setOnChangeCallBack(new cutomeView.OnChangeCallBack() {
@Override
public void change(boolean isexprand) {
isExprand = isexprand;
}
});
注意:
expandDrable = a.getDrawable(R.styleable.cutomeView_expand_drawable);
collapseDrawable = a.getDrawable(R.styleable.cutomeView_collapse_drawable);
if (expandDrable != null) {
expandDrable.setBounds(0, 0, 48, 24);
}
if (collapseDrawable != null) {
collapseDrawable.setBounds(0, 0, 48, 24);
}
drawable 必须设置setBounds 不然现实不出来,只有设置固定大小,就可以现实出来。
后期对折叠空间进行了扩展,现在需求中很多的需求,
1 当显示的内容不大于设定行数,添加设定行数的属性,
2 对于折叠控件的大小样式进行扩展
package com.example.bkhu.myapplication;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.DynamicLayout;
import android.text.Layout;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.bkhu.myapplication.R;
/**
* Created by bkhu on 17/1/2.
*/
public class HotelCustomExpandView extends LinearLayout implements View.OnClickListener {
private boolean isExpand = false;
private TextView contentView = null;
private TextView expandText = null;
private int mMaxCollapseLine = 2;
private Drawable expandDrawable = null;
private Drawable collapseDrawable = null;
private int mContentTextStyle;
private int mExpandTextStyle;
private String mExpandTextTitle;
private String mCollapseTextTitle;
private boolean onClick;
public HotelCustomExpandView(Context context) {
this(context, null);
}
public HotelCustomExpandView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public HotelCustomExpandView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.hote