android自定义属性

 一、在res/values/attrs.xml文件中:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!--声明一个属性,名称可以随意,一般将名称 与拥有这些属性的类名相同  -->
    <declare-styleable name="SettingView">
        <attr name="title" format="string" />    
        <attr name="desc" format="string" />    
    </declare-styleable>    
</resources>

    二、在布局文件中使用声明的属性
        1、声明命名空间:
             xmlns:itheima=" http://schemas.android.com/apk/res/com.itheima.mobilesafe45 "
        2、给view添加自定的属性:
            <com.itheima.mobilesafe45.view.SettingView
                    android:id="@+id/sv_autoUpdate"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                     itheima:title="自动更新设置"
                    itheima:desc="自动更新已经打开#自动更新已经关闭"        
        />

    三、在代码中解析,获得这些属性
//        attrs.getAttributeCount();
//        attrs.getAttributeName();
//        attrs.getAttributeValue();

        String title = attrs.getAttributeValue(" http://schemas.android.com/apk/res/com.itheima.mobilesafe45","title");
        String desc = attrs.getAttributeValue(" http://schemas.android.com/apk/res/com.itheima.mobilesafe45","desc");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值