欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android中自定義控件的declare-styleable屬性重用方案

 更新時(shí)間:2015年01月19日 09:15:14   投稿:junjie  
這篇文章主要介紹了Android中自定義控件的declare-styleable屬性重用方案,本文給出了一個(gè)終極重用解決方案,需要的朋友可以參考下

最近接觸了Android自定義控件,涉及到自定義xml中得屬性(attribute),其實(shí)也很簡(jiǎn)單,但是寫著寫著,發(fā)現(xiàn)代碼不完美了,就是在attrs.xml這個(gè)文件中,發(fā)現(xiàn)屬性冗余,于是就想有沒有類似屬性繼承或者include之類的方法.本文將就declare-stylable中屬性重用記錄一下.

不完美的代碼

復(fù)制代碼 代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
 
    <declare-styleable name="ExTextView">
       <attr name="enableOnPad" format="boolean" />
     <attr name="supportDeviceType" format="reference"/>
    </declare-styleable>

    <declare-styleable name="ExEditText">
       <attr name="enableOnPad" format="boolean" />
     <attr name="supportDeviceType" format="reference"/> 
    </declare-styleable>
</resources>

如上面代碼,在ExTextView和ExEditText這個(gè)stylable中有著重復(fù)的屬性申明.雖然上面可以工作,但是總感覺寫的不專業(yè),于是尋找優(yōu)化方法.

這樣可以么

嘗試著為declare-stylable指定一個(gè)parent,如下代碼

復(fù)制代碼 代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
 
    <declare-styleable name="ExTextView">
       <attr name="enableOnPad" format="boolean" />
     <attr name="supportDeviceType" format="reference"/>
    </declare-styleable>

    <declare-styleable name="ExEditText" parent="ExTextView">
         
    </declare-styleable>
</resources>


attrs.xml沒有報(bào)告語(yǔ)法錯(cuò)誤.但是當(dāng)我使用R.styleable.ExEditText_supportDeviceType時(shí)候,R文件卻沒有生成,重新清理了工程還是不生效,不知道是否為adt插件的問(wèn)題.其他人也遇到了這樣的問(wèn)題. 這個(gè)方法目前是不行的.

終極答案

實(shí)際上我們可以在declare-stylable之前,申明要多次使用的屬性,在declare-stylable節(jié)點(diǎn)內(nèi)部,只需調(diào)用即可.具體代碼如下.

復(fù)制代碼 代碼如下:

<?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>


每次引用attr后,建議清理一下工程,確保R文件重新生成.

延伸閱讀

http://stackoverflow.com/questions/18827875/how-to-declare-several-stylable-attributes-with-the-same-name-for-different-tags

相關(guān)文章

最新評(píng)論