今天開始要自我學習自定義控件了,第一步瞭解attr,借鑑博客: http://huangbo-2020.iteye.com/blog/1477611 http://www.jianshu.com/p/813772e748c6 - 1. reference:表示attr的值只能指向某一資源的ID,例如取值@id/textView。 (1)屬性定義: <declare-styleable name = "名稱"> <attr name = "background" format = "reference" /> </declare-styleable> (2)屬性使用: <ImageView android:layout_width = "42dp" android:layout_height = "42dp" android:background = "@drawable/圖片ID" /> - 2.表示attr是顏色類型,例如#ff0000,也能夠使用一個指向Color的資源,好比[@android](https://my.oschina.net/asia):color/background_dark,可是不能用0xffff0000這樣的值。 (1)屬性定義: <declare-styleable name = "名稱"> <attr name = "textColor" format = "color" /> </declare-styleable> (2)屬性使用: <TextView android:layout_width = "42dp" android:layout_height = "42dp" android:textColor = "#00FF00" /> 3.-boolean:表示attr是布爾類型的值,取值只能是true或false。 (1)屬性定義: <declare-styleable name = "名稱"> <attr name = "focusable" format = "boolean" /> </declare-styleable> (2)屬性使用: <Button android:layout_width = "42dp" android:layout_height = "42dp" android:focusable = "true" /> - 4.dimension:表示attr是尺寸類型,例如取值16px、16dp,也能夠使用一個指向類型的資源 (1)屬性定義: <declare-styleable name = "名稱"> <attr name = "layout_width" format = "dimension" /> </declare-styleable> (2)屬性使用: <Button android:layout_width = "42dp" android:layout_height = "42dp" /> 5.- float:表示attr是浮點數類型,取值只能是浮點數或整數。 (1)屬性定義: <declare-styleable name = "AlphaAnimation"> <attr name = "fromAlpha" format = "float" /> <attr name = "toAlpha" format = "float" /> </declare-styleable> (2)屬性使用: <alpha android:fromAlpha = "1.0" android:toAlpha = "0.7" /> 6.- integer:表示attr是整數類型,取值只能是整數,不能是浮點數。 <declare-styleable name = "AnimatedRotateDrawable"> <attr name = "visible" /> <attr name = "frameDuration" format="integer" /> <attr name = "framesCount" format="integer" /> <attr name = "pivotX" /> <attr name = "pivotY" /> <attr name = "drawable" /> </declare-styleable> (2)屬性使用: <animated-rotate xmlns:android = "http://schemas.android.com/apk/res/android" android:drawable = "@drawable/圖片ID" android:pivotX = "50%" android:pivotY = "50%" android:framesCount = "12" android:frameDuration = "100" /> 7.- string:表示attr是字符串類型。 (1)屬性定義: <declare-styleable name = "MapView"> <attr name = "apiKey" format = "string" /> </declare-styleable> (2)屬性使用: <com.google.android.maps.MapView android:layout_width = "fill_parent" android:layout_height = "fill_parent" android:apiKey = "gdbfdbt968qdsfw" /> - 8.fraction:表示attr是百分數類型,取值只能以%結尾,例如30%、120.5%等。 (1)屬性定義: <declare-styleable name="RotateDrawable"> <attr name = "visible" /> <attr name = "fromDegrees" format = "float" /> <attr name = "toDegrees" format = "float" /> <attr name = "pivotX" format = "fraction" /> <attr name = "pivotY" format = "fraction" /> <attr name = "drawable" /> </declare-styleable> (2)屬性使用: <rotate xmlns:android = "http://schemas.android.com/apk/res/android" android:interpolator = "@anim/動畫ID" android:fromDegrees = "0" android:toDegrees = "360" android:pivotX = "200%" android:pivotY = "300%" android:duration = "5000" android:repeatMode = "restart" android:repeatCount = "infinite" /> 9.- enum:表示attr是枚舉類型,在定義enum類型的attr時,能夠將attr的format設置爲enum,也能夠不用設置attr的format屬性,可是必須在attr節點下面添加一個或多個enum節點。取值時只能取其中一個枚舉值 (1)屬性定義: <declare-styleable name="名稱"> <attr name="orientation"> <enum name="horizontal" value="0" /> <enum name="vertical" value="1" /> </attr> </declare-styleable> (2)屬性使用: <LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:orientation = "vertical" android:layout_width = "fill_parent" android:layout_height = "fill_parent" > </LinearLayout> 10.- flag:表示attr是bit位標記,flag與enum有類似之處,定義了flag的attr,在設置值時,能夠經過|設置多個值,並且每一個值都對應一個bit位,這樣經過按位或操做符|能夠將多個值合成一個值,咱們通常在用flag表示某個字段支持多個特性,須要注意的是,要想使用flag類型,不能在attr上設置format爲flag,不要設置attr的format的屬性,直接在attr節點下面添加flag節點便可。 (1)屬性定義: <declare-styleable name="名稱"> <attr name="windowSoftInputMode"> <flag name = "stateUnspecified" value = "0" /> <flag name = "stateUnchanged" value = "1" /> <flag name = "stateHidden" value = "2" /> <flag name = "stateAlwaysHidden" value = "3" /> <flag name = "stateVisible" value = "4" /> <flag name = "stateAlwaysVisible" value = "5" /> <flag name = "adjustUnspecified" value = "0x00" /> <flag name = "adjustResize" value = "0x10" /> <flag name = "adjustPan" value = "0x20" /> <flag name = "adjustNothing" value = "0x30" /> </attr> </declare-styleable> (2)屬性使用: <activity android:name = ".StyleAndThemeActivity" android:label = "@string/app_name" android:windowSoftInputMode = "stateUnspecified | stateUnchanged | stateHidden"> <intent-filter> <action android:name = "android.intent.action.MAIN" /> <category android:name = "android.intent.category.LAUNCHER" /> </intent-filter> </activity> flag表示這幾個值能夠作或運算,好比上面的dogFood,你能夠疊加使用,狗狗食物可同時選牛肉、豬肉等。而狗狗的性別只能有一種。 注意: 屬性定義時能夠指定多種類型值。 (1)屬性定義: <declare-styleable name = "名稱"> <attr name = "background" format = "reference|color" /> </declare-styleable> (2)屬性使用: <ImageView android:layout_width = "42dp" android:layout_height = "42dp" android:background = "@drawable/圖片ID|#00FF00" />