android自定义组件

废话不说直接上图(效果)
在这里插入图片描述
开发过程中经常会碰到这样的通用布局,显示不同功能的入口 例如左边一个图片 接一个文案 中间一个要显示的字段 右边一个要显示的字段 再加一个右箭头,虽然在布局中代码很简单,但是难免会显得代码多影响阅读,那么这个时候我们可以自定义一个通用组件CommonView

1.在attrs中定义一个styleable

<attr name="isShowTextNotes" format="boolean" />
<attr name="isSingleTextNotes" format="boolean" />
<attr name="notesText" format="string" />
<attr name="notesTextColor" format="color" />
<attr name="notesTextSize" format="dimension" />

<attr name="isShowTextCent" format="boolean" />
<attr name="isSingleTextCent" format="boolean" />
<attr name="centText" format="string" />
<attr name="centTextColor" format="color" />
<attr name="centTextSize" format="dimension" />

<attr name="isShowArraw" format="boolean" />

2.自定义组件

/**
 * 自定义通用控件
 * Created by wangbin on 16/2/15.
 */
public class CommonView extends RelativeLayout {

    private ImageView ivCommon;//左边图表
    private TextView tvCommon;//左边文字
    private TextView tvNotes;//右边按钮文字
    private TextView tvNotesBottom;//右边按钮底部文字
    private ImageView ivArrow;//右边箭头
    private TextView tv_centtext;//左边文字邮编的描述
    private RelativeLayout rl_bg;//整个布局


    /**
     * 字体颜色
     */
    private int desTextColor;
    /**
     * 分类的图片
     */
    private Drawable categoryImage;
    /**
     * 文字描述
     */
    private String desText;
    /**
     * 文字大小 默认16
     */
    private float desTextSize;
    /**
     * 是否显示分类图片
     */
    private boolean isShowCategoryImage;
    /**
     * 分类的状态图片
     */
    private Drawable statusImage;
    /**
     * 右边箭头图片
     */
    private int ivImage;
    /**
     * 分类的状态图片的高 默认16
     */
    private float statusImage_width;
    /**
     * 分类的状态图片的宽 默认16
     */
    private float statusImage_height;

    /**
     * 是否显示最邮编文字
     */
    private boolean isShowTextNotes;
    private String notesText;
    private int notesTextColor;
    private float notesTextSize;
    private boolean flag;
    private boolean tvNotesSingleLine;


    private boolean isShowArraw;//显示邮编箭头


    /**
     * 是否显示最左边文字邮编的文字
     */
    private boolean isShowTextcent;
    private String centText;
    private int centextColor;
    private float centTextSize;
    private boolean centflag;
    private boolean tvcentSingleLine;




    public CommonView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
        TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.CommonView);

        //左边字体
        desText = mTypedArray.getString(R.styleable.CommonView_desText);//左边字体
        desTextColor = mTypedArray.getColor(R.styleable.CommonView_desTextColor,
               getResources().getColor(R.color.black_1));//左边字体颜色

        desTextSize = mTypedArray.getDimensionPixelSize(
                R.styleable.CommonView_desTextSize, 28);//字体大小

        isShowCategoryImage = mTypedArray.getBoolean(
                R.styleable.CommonView_isShowCategoryImage, true);//是否显示左边图片(这里没用到)

        statusImage = mTypedArray.getDrawable(R.styleable.CommonView_statusImage);//左边图片

        //右边字体
        isShowTextNotes = mTypedArray.getBoolean(R.styleable.CommonView_isShowTextNotes, true);//是否显示
        notesText = mTypedArray.getString(R.styleable.CommonView_notesText);
        notesTextColor = mTypedArray.getColor(R.styleable.CommonView_notesTextColor, getResources().getColor(R.color.black_1));
        notesTextSize = mTypedArray.getDimensionPixelSize(R.styleable.CommonView_notesTextSize, 28);
        tvNotesSingleLine=mTypedArray.getBoolean(R.styleable.CommonView_isSingleTextNotes,true);

        //中间字体
        isShowTextcent = mTypedArray.getBoolean(R.styleable.CommonView_isShowTextCent, false);//是否显示
        centText = mTypedArray.getString(R.styleable.CommonView_centText);
        centextColor = mTypedArray.getColor(R.styleable.CommonView_centTextColor, getResources().getColor(R.color.black_1));
        centTextSize = mTypedArray.getDimensionPixelSize(R.styleable.CommonView_centTextSize, 28);
        tvcentSingleLine=mTypedArray.getBoolean(R.styleable.CommonView_isSingleTextCent,true);

        //右边箭头
        isShowArraw=mTypedArray.getBoolean(R.styleable.CommonView_isShowArraw,true);




        mTypedArray.recycle();

        initData();

    }

    private void init() {
        View view = View.inflate(getContext(), R.layout.common_view, this);
        ivCommon = (ImageView) view.findViewById(R.id.ivCommon);//左边图片
        tvCommon = (TextView) view.findViewById(R.id.tvCommon);//左边文字
        tvNotes = (TextView) view.findViewById(R.id.tvNotes);//右边文字
        tvNotesBottom =  (TextView) view.findViewById(R.id.tvNotesBottom);//右边底部文字
        ivArrow =(ImageView) view.findViewById(R.id.ivArrow);//右边箭头
        tv_centtext= (TextView) view.findViewById(R.id.tv_centtext);//中间文字
    }
    public void setTvNotesBottomVisiable(int visiable){
        tvNotesBottom.setVisibility(visiable);
    }
    /**
     * 设置分类状态图片的宽和高
     *
     * @param statusImage_width  单位是dp 无需自己转换
     * @param statusImage_height 单位是dp 无需自己转换
     */
    public void setStatusImageWH(float statusImage_width,
                                 float statusImage_height) {
        android.view.ViewGroup.LayoutParams params = ivCommon
                .getLayoutParams();
        params.width = (int) statusImage_width;
        ivCommon.setLayoutParams(params);
        params.height = (int) statusImage_height;
        this.statusImage_width = statusImage_width;
        this.statusImage_height = statusImage_height;
    }

    /**
     * 设置分类状态的图片
     *
     * @param statusImage
     */
    @SuppressWarnings("deprecation")
    public void setStatusImage(Drawable statusImage) {
        if (statusImage != null) {
            ivCommon.setBackgroundDrawable(statusImage);
            this.statusImage = statusImage;
        }

    }

    /**
     * 设置是否显示分类图片
     *
     * @param isShowCategoryImage
     */
    public void setIsShowCategoryImage(boolean isShowCategoryImage) {
        if (isShowCategoryImage) {
            ivCommon.setVisibility(View.VISIBLE);
        } else {
            ivCommon.setVisibility(View.GONE);
        }
        this.isShowCategoryImage = isShowCategoryImage;

    }

    /**
     * 设置描述字体的大小
     *
     * @param desTextSize 单位是sp
     */
    public void setDesTextSize(float desTextSize) {
        tvCommon.setTextSize(TypedValue.COMPLEX_UNIT_PX, desTextSize);
        // tv_des.setTextSize( desTextSize);
        this.desTextSize = desTextSize;

    }

    /**
     * 设置描述信息
     *
     * @param desText
     */
    public void setDesText(String desText) {
        if (desText != null) {
            tvCommon.setText(desText);
            this.desText = desText;
        }
    }

    /**
     * 设置 分类的图片
     */
//    @SuppressWarnings("deprecation")
//    public void setCategoryImage(Drawable categoryImage) {
//        if (categoryImage != null) {
//            iv_category.setBackgroundDrawable(categoryImage);
//            this.categoryImage = categoryImage;
//        }
//
//    }

    /**
     * 设置描述字体颜色
     *
     * @param desTextColor
     */
    public void setDesTextColor(int desTextColor) {
        tvCommon.setTextColor(desTextColor);
        this.desTextColor = desTextColor;
    }

    /**
     * 设置右边文字
     * @param notesText
     */
    public void setNotesText(String notesText) {
        if (notesText != null) {
            tvNotes.setText(notesText);
            this.notesText = notesText;
        }
    }

    /**
     * 设置中间文字
     * @param centText
     */
    public void setcentText(String centText) {
        if (centText != null) {
            tv_centtext.setText(centText);
            this.centText = centText;
        }
    }



    /**
     * 设置右边文字颜色
     * @param notesTextColor
     */
    public void setNotesTextColor(int notesTextColor){
        tvNotes.setTextColor(notesTextColor);
        this.notesTextColor=notesTextColor;
    }

    /**
     * 设置中间文字颜色
     * @param centTextColor
     */
    public void setCentTextColor(int centTextColor){
        tv_centtext.setTextColor(centTextColor);
        this.centextColor=centTextColor;
    }


    public void setSingleLine(boolean singleLine){
        tvNotes.setSingleLine(singleLine);
        this.tvNotesSingleLine=singleLine;
    }

    public void setcentSingleLine(boolean singleLine){
        tv_centtext.setSingleLine(singleLine);
        this.tvcentSingleLine=singleLine;
    }

    /***
     * 设置右边箭头可见不可见
     * @param flag
     */
    public void setIvArrow(boolean flag){
        if (flag==true){
            ivArrow.setVisibility(View.VISIBLE);
            setEnabled(true);
        }else {
            ivArrow.setVisibility(View.GONE);
            setEnabled(false);
        }
        this.isShowArraw=flag;
    }





    /**
     * 设置字体大小
     * @param notesTextSize
     */
    public void setNotesTextSize(float notesTextSize){
        tvNotes.setTextSize(TypedValue.COMPLEX_UNIT_PX, notesTextSize);
        this.notesTextSize=notesTextSize;
    }

    /**
     * 设置字体大小
     * @param centTextSize
     */
    public void setcentTextSize(float centTextSize){
        tv_centtext.setTextSize(TypedValue.COMPLEX_UNIT_PX, centTextSize);
        this.centTextSize=centTextSize;
    }

    public void setIsShowNotesText(boolean isShowTextNotes){
        if(isShowTextNotes){
            tvNotes.setVisibility(View.VISIBLE);
        }else{
            tvNotes.setVisibility(View.GONE);
        }
        this.isShowTextNotes=isShowTextNotes;
    }


    /**
     * 设置中间文字是否课件
     * @param isShowTextCent
     */
    public void setIsShowCentText(boolean isShowTextCent){
        if(isShowTextCent){
            tv_centtext.setVisibility(View.VISIBLE);
        }else{
            tv_centtext.setVisibility(View.GONE);
        }
        this.isShowTextcent=isShowTextCent;
    }




    /**
     * 初始化数据参数
     */
    private void initData() {

        setDesTextColor(desTextColor);
        setDesText(desText);
        setDesTextSize(desTextSize);
        setIsShowCategoryImage(isShowCategoryImage);
        setStatusImage(statusImage);

        setNotesText(notesText);
        setNotesTextColor(notesTextColor);
        setNotesTextSize(notesTextSize);
        setIsShowNotesText(isShowTextNotes);
        setSingleLine(tvNotesSingleLine);

        setcentSingleLine(tvcentSingleLine);
        setcentText(centText);
        setcentTextSize(centTextSize);
        setCentTextColor(centextColor);
        setIsShowCentText(isShowTextcent);

        setIvArrow(isShowArraw);
    }
}

3.在对应的布局文件中申明自定义的布局

 <com.rmwl.rcchgwd.view.CommonView
            android:id="@+id/com_cpgm"
            app:notesTextSize="13sp"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            app:desText="产品规模"
            app:desTextColor="@color/black_3"
            app:desTextSize="13sp"
            app:isShowArraw="false"
            app:isShowCategoryImage="false"
            app:isShowTextCent="false"
            app:isShowTextNotes="true"
            app:notesText=""
            app:notesTextColor="@color/black_1"></com.rmwl.rcchgwd.view.CommonView>

3.总结自定义这样的组件逻辑非常简单通首先在attrs中定一个styleable,然后里面写上你需要的attrs,在自定义组件中通过TypeArray类去找到你定义的属性分别给个默认值然后 mTypedArray.recycle()下,后面就可以在布局中引用了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值