自定义属性的使用方法

这段时间因为接触到了自定义的控件,无意间看到了自定义的属性的用法,但是在网上搜到的使用方法是这种的:

在res中定义一个合法命名的xml文件,在其中写入自己想要自定义的属性,注意节点名依次为<resources>和<declare-styleable name=yourname>;

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="ToolBar">
        <attr name="buttonNum" format="integer"/>
        <attr name="itemBackground" format="reference|color"/>
    </declare-styleable>
</resources>

然后在需要使用的地方定义xmlns,

xmlns:toolbar="http://schemas.android.com/apk/res/cn.zzm.toolbar"

这个cn.zzm.toolbar是最上层的包名,一般情况下为AndroidManifest.xml中的package名字;


当然,我也见到过另外一种写法,这种写法是表示自动导入的app为命名空间的

xmlns:app="http://schemas.android.com/apk/res-auto"

这个对应的属性文件内容为:

<resources>
    <declare-styleable name="RotateTextView">
        <attr name="degree" format="dimension" />
    </declare-styleable>
</resources>

对应的layout文件为:

<com.idunnololz.widgets.RotateTextView
        xmlns:app="http://schemas.android.com/apk/res-auto"  
        android:id="@+id/status"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:gravity="center"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:text="热热热"
        android:textColor="#000000" />

这种写法也挺方便的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值