Android自定义属性的使用步骤

本文详细介绍了Android中自定义属性的使用方法,包括如何声明属性、在布局文件中使用属性及解析属性等内容。通过示例代码展示了具体实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android自定义属性的使用


一、参考资料

	有关自定义属性,我们可以参考系统控件是如何使用自定义属性的。系统有关自定义属性的文件在SDK中有,具体的路径如下:
..\SDK\platforms\android-xx\data\res\values\attrs文件中可以看到系统声明的自定义属性。
下边截取一段系统的自定义属性的使用:
 <declare-styleable name="ListView">
        <!-- Reference to an array resource that will populate the ListView.  For static content,
             this is simpler than populating the ListView programmatically. -->
        <attr name="entries" />
        <!-- Drawable or color to draw between list items. -->
        <attr name="divider" format="reference|color" />
        <!-- Height of the divider. Will use the intrinsic height of the divider if this
             is not specified. -->
        <attr name="dividerHeight" format="dimension" />
        <!-- When set to false, the ListView will not draw the divider after each header view.
             The default value is true. -->
        <attr name="headerDividersEnabled" format="boolean" />
        <!-- When set to false, the ListView will not draw the divider before each footer view.
             The default value is true. -->
        <attr name="footerDividersEnabled" format="boolean" />
        <!-- Drawable to draw above list content. -->
        <attr name="overScrollHeader" format="reference|color" />
        <!-- Drawable to draw below list content. -->
        <attr name="overScrollFooter" format="reference|color" />
    </declare-styleable>

二、自定义属性使用的步骤

1、声明属性:

直接上代码,代码中有具体的解释。values/attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- 声明一个属性集合,名称一般和拥有这些属性的类同名 -->
    <declare-styleable name="MyToggleBtn" >
        <!-- 声明背景属性,格式是引用类型 -->
        <attr name="bg_bitmap" format="reference" />
        <!-- 声明滑动图片属性,格式是引用类型 -->
        <attr name="slide_bitmap" format="reference" />
    </declare-styleable>
</resources>

2、在布局中使用这些属性:

1)声明一个命名空间:

首先来看一下系统声明的命名空间,在layout文件中的根布局中会有这样一行代码:
//其中xml后的ns代码namespace命名空间,名字为android
xmlns:android="http://schemas.android.com/apk/res/android"
我们需要声明自己的命名空间:
xmlns:myview="http://schemas.android.com/apk/res/包名"(也就是将其中的android换成自己定义的一个名字,在将后边的部分改为自己的包名就行了)
注:在Android Studio中这样使用会报错的:
in Gradle projects,always usehttp://schemas.android.com/apk/res-auto for custom attributes
在Android Studio中我们只需要这样写就行了 :
xmlns:myview="http://schemas.android.com/apk/res-auto"(这样Gradle就会自动查找自定属性的)

2)使用这些属性

   例如我在布局文件中是这样使用的:	
<project.hjking.cn.myview.mytogglebtn.MyToggleBtn
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        myview:bg_bitmap="@drawable/switch_background"
        myview:slide_bitmap="@drawable/slide_button"/>

3)、在自定义View的构造函数中解析这些属性,并使用

 /**
     * 这个构造函数是在xml布局文件中使用该view时会调用这个构造方法
     *
     * @param context
     * @param attrs
     */
    public MyToggleBtn(Context context, AttributeSet attrs) {
        super(context, attrs);
       /*
        //可以遍历获取自定义控件的所有属性
       //获得属性的个数
        int count = attrs.getAttributeCount();
        for (int i = 0; i < count; i++) {
            String attrName = attrs.getAttributeName(i);
            String attrValue = attrs.getAttributeValue(i);
        }*/

        TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.MyToggleBtn);
        //获取背景资源
        BitmapDrawable bgDrawable = (BitmapDrawable) typedArray.getDrawable(R.styleable.MyToggleBtn_bg_bitmap);
        bgBitmap = bgDrawable.getBitmap();
        BitmapDrawable slidDrawable = (BitmapDrawable) typedArray.getDrawable(R.styleable.MyToggleBtn_slide_bitmap);
        slidBitmap = slidDrawable.getBitmap();
    }

具体的项目地址:https://github.com/OONullPointerAlex/MyViewProject






                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值