關於自定義view,相比大家已經很熟悉了。作者也是最近發現了一些好玩的自定義view時,才重新整理下這方面的知識,大部分來源於前輩們的文章。
一、需要使用xml來定義View的屬性,即在res/values文件下定義一個attrs.xml文件,代碼如下:
二、在佈局中使用該屬性:
三、在代碼中獲取自定義的值:
*********************************************************************
好了,基本用法已經講完了,現在來看看一些注意點和知識點吧。
首先來看看attrs.xml文件。
該文件是定義屬性名和格式的地方,需要用<declare-styleable name="QQNaviView"></declare-styleable>包圍所有屬性。其中name爲該屬性集的名字,主要用途是標識該屬性集。那在
什麼地方會用到呢?主要是在第三步。看到沒?在獲取某屬性標識時,用到"R.styleable.QQNaviView_buttonNum",很顯然,他在每個屬性前面都加了"QQNaviView_"。
在來看看各種屬性都有些什麼類型吧:string , integer , dimension , reference , color , enum......
前面幾種的聲明方式都是一致的,例如:<attr name="buttonNum" format="integer"/>。
只有enum是不同的,用法舉例:
如果該屬性可同時傳兩種不同的屬性,則可以用“|”分割開即可。
讓我們再來看看佈局xml中需要注意的事項。
首先得聲明一下:xmlns:toolbar=http://schemas.android.com/apk/res/com.robog.library.QQNaviView
注意,“QQNaviView”可以換成其他的任何名字,後面的url地址必須最後一部分必須用上自定義組件的包名。自定義屬性了,在屬性名前加上“QQNaviView”即可。
最後來看看java代碼中的注意事項。
在自定義組件的構造函數中,用
TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.QQNaviView);
來獲得對屬性集的引用,然後就可以用“a”的各種方法來獲取相應的屬性值了。這裏需要注意的是,如果使用的方法和獲取值的類型不對的話,則會返回默認值。因此,如果一個屬
性是帶兩個及以上不用類型的屬性,需要做多次判斷,知道讀取完畢後才能判斷應該賦予何值。當然,在取完值的時候別忘了回收資源哦!
自定義屬性數據類型簡介:
一、reference:參考指定Theme中資源ID。
1.定義:
<declare-styleable name="My">
<attr name="label" format="reference" >
</declare-styleable>
2.使用:
<Buttonzkx:label="@string/label" >
二、Color:顏色
1.定義:
<declare-styleable name="My">
<attr name="textColor" format="color" />
</declare-styleable>
2.使用:
<Button zkx:textColor="#ff0000"/>
三、boolean:布爾值
1.定義:
<declare-styleable name="My">
<attr name="isVisible" format="boolean" />
</declare-styleable>
2.使用:
<Button zkx:isVisible="false"/>
四、dimension:尺寸值
1.定義:
<declare-styleable name="My">
<attr name="myWidth" format="dimension" />
</declare-styleable>
2.使用:
<Button zkx:myWidth="100dip"/>
五、float:浮點型
1.定義:
<declare-styleable name="My">
<attr name="fromAlpha" format="float" />
</declare-styleable>
2.使用:
<alpha zkx:fromAlpha="0.3"/>
六、integer:整型
1.定義:
<declare-styleable name="My">
<attr name="frameDuration" format="integer" />
</declare-styleable>
2.使用:
<animated-rotate zkx:framesCount="22"/>
七、string:字符串
1.定義:
<declare-styleable name="My">
<attr name="Name" format="string" />
</declare-styleable>
2.使用:
<rotate zkx:pivotX="200%"/>
八、fraction:百分數
1.定義:
<declare-styleable name="My">
<attr name="pivotX" format="fraction" />
</declare-styleable>
2.使用:
<rotate zkx:Name="My name is zhang kun xiang"/>
九、enum:枚舉
1.定義:
<declare-styleable name="My">
<attr name="language">
<enum name="English" value="1"/>
</attr>
</declare-styleable>
2.使用:
<Button zkx:language="English"/>
十、flag:位或運算
1.定義:
<declare-styleable name="My">
<attr name="windowSoftInputMode">
<flag name="stateUnspecified" value="1" />
<flag name = "adjustNothing" value = "0x30" />
</attr>
</declare-styleable>
2.使用:
<activity Android:windowSoftInputMode="stateUnspecified | adjustNothing">
屬性定義時可以指定多種類型值:
<declare-styleable name = "名稱">
<attr name="background" format="reference|color" />
</declare-styleable>
使用:
<ImageView android:background = "@drawable/圖片ID|#00FF00"/>