Android自定义View

源码Demo:请点击此处

前言

在我们开发中,自定义View是非常常用的,这篇文章主要是和大家分享一下:如何自定义一个简单的组合布局(我的RelativeLayout)

接下来:我将分解每一个步骤,并附带简单的代码说明

  • 1. 在 res/values 目录下新建一个 attrs.xml 文件,并定义一些属性
    <!--<declare-styleable name="自定义属性名称">-->
    <!--<attr name="属性名称" format="属性类型" />-->
    <declare-styleable name="ItemRelativeView">
        <attr name="itemHeight" format="dimension"/>
        <attr name="leftDrawable" format="reference"/>
        <attr name="leftText" format="string"/>
        <attr name="rightText" format="string"/>
    </declare-styleable>
  • 2. 在自定义组件代码中,获取 attrs.xml 文件中定义的属性
    private void initAttributes(Context context, AttributeSet attrs, int defStyleAttr) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ItemRelativeView);
        //TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ItemRelativeView, defStyleAttr, 0);
        itemHeight = typedArray.getDimension(R.styleable.ItemRelativeView_itemHeight, dip2px(context, 48));
        leftDrawableId = typedArray.getResourceId(R.styleable.ItemRelativeView_leftDrawable, View.NO_ID);
        leftTextContent = typedArray.getString(R.styleable.ItemRelativeView_leftText);
        rightTextContent = typedArray.getString(R.styleable.ItemRelativeView_rightText);
        typedArray.recycle();//typedArray用完之后需要回收,防止内存泄漏
    }
  • 3. 获取自定义布局控件,将自定义的属性值设置给对应的控件
 private void inflateView(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.view_custom_relative, this, true);
        RelativeLayout layoutView = view.findViewById(R.id.layoutView);
        ImageView ivInfo = view.findViewById(R.id.ivInfo);
        TextView tvInfo = view.findViewById(R.id.tvInfo);
        tvRight = view.findViewById(R.id.tvRight);
        //给自定义布局设置属性传递的数据
        ViewGroup.LayoutParams params = layoutView.getLayoutParams();
        params.height = (int) itemHeight;
        layoutView.setLayoutParams(params);
        if (leftDrawableId > 0) {
            ivInfo.setImageResource(leftDrawableId);
        }
        if (TextUtils.isEmpty(leftTextContent)) {
            tvInfo.setVisibility(INVISIBLE);
        } else {
            tvInfo.setVisibility(VISIBLE);
            tvInfo.setText(leftTextContent);
        }
        if (TextUtils.isEmpty(rightTextContent)) {
            tvRight.setVisibility(INVISIBLE);
        } else {
            tvRight.setVisibility(VISIBLE);
            tvRight.setText(rightTextContent);
        }
        this.setEnabled(true);
        this.setClickable(true);
    }
  • 4. 在布局xml中使用该属性
    <com.zero.custom.view.ItemRelativeView
            android:id="@+id/itemRelativeView"
            android:layout_width="match_parent"
            app:itemHeight="56dp"
            app:leftDrawable="@mipmap/ic_launcher"
            app:leftText="左边"
            app:rightText="右边"
            android:layout_marginTop="20dp"
            android:layout_height="wrap_content"/>

自定义属性以及格式列举

  • 自定义属性数据类型简介
    ① reference:参考指定Theme中资源ID。
    ② Color:颜色
    ③ boolean:布尔值
    ④ dimension:尺寸值
    ⑤ float:浮点型
    ⑥ integer:整型
    ⑦ string:字符串
    ⑧ fraction:百分数
    ⑨ enum:枚举
    ⑩ flag:位或运算

  • 格式说明

    <declare-styleable name="自定义属性名称">
        <attr name="属性名称" format="属性类型"/>
    </declare-styleable>

    <declare-styleable name="My">
        <attr name="label" format="reference"/>
        <attr name="textColor" format="color"/>
        <attr name="isVisible" format="boolean"/>
        <attr name="myWidth" format="dimension"/>
        <attr name="fromAlpha" format="float"/>
        <attr name="frameDuration" format="integer"/>
        <attr name="Name" format="string"/>
        <attr name="pivotX" format="fraction"/>
        <attr name="language">
            <enum name="English" value="1"/>
        </attr>
        <attr name="windowSoftInputMode">
            <flag name="stateUnspecified" value="1"/>
            <flag name="adjustNothing" value="0x30"/>
        </attr>
    </declare-styleable>

    <!--属性定义时可以指定多种类型值-->
    <declare-styleable name="名称">
        <attr name="background" format="reference|color"/>
    </declare-styleable>
原文地址:https://www.cnblogs.com/io1024/p/11599394.html