自定义view的细节(一)declare-styleable属性重用方案

参考了:http://www.jb51.net/article/60013.htm 作者:junjie

向开源,分享致敬。

在日常的自定义view中,经常会用到declare-styleable属性,先看看如下代码

<span style="font-family:Arial Black;"><?xml version="1.0" encoding="utf-8"?>
<resources>
  
    <declare-styleable name="ExTextView">
       <attr name="enableOnPad" format="boolean" />
     <attr name="supportDeviceType" format="reference"/>
    </declare-styleable></span>
<span style="font-family:Arial Black;">
    <declare-styleable name="ExEditText">
       <attr name="enableOnPad" format="boolean" />
     <attr name="supportDeviceType" format="reference"/>  
    </declare-styleable>
</resources></span>

这是一段可以使用的代码,在ExEditText中与ExTextView中有着重复的属性申明,虽然可以正常工作,但是代码简洁度不够,而且重复的事情不是很好,考虑优化。

实际上我们可以在declare-stylable之前,申明要多次使用的属性,在declare-stylable节点内部,只需调用即可.具体代码如下.

<span style="font-family:Arial Black;"><?xml version="1.0" encoding="utf-8"?>
<resources>
  <attr name="enableOnPad" format="boolean" />
  <attr name="supportDeviceType" format="reference"/>
  
    <declare-styleable name="ExTextView">
        <attr name="enableOnPad"/>
        <attr name="supportDeviceType"/>
    </declare-styleable>
    <declare-styleable name="ExEditText">
        <attr name="enableOnPad"/>
      <attr name="supportDeviceType"/> 
    </declare-styleable>
</resources></span>


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值