android具有折叠效果的控件

本文介绍了一个自定义的Android控件,用于实现具有折叠效果。控件包含TextView,可扩展行数,并允许自定义展开和折叠图标。在使用过程中需要注意Drawable的setBounds设置,以及对控件的扩展和属性配置。文章提到了在不同情况下TextView布局的构建,如StaticLayout和DynamicLayout的使用。
摘要由CSDN通过智能技术生成

在工作中有时候遇到这样的效果
这里写图片描述

这里写图片描述

源码如下:
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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值