Android自定义属性 format的深入解析
程序员文章站
2024-01-27 10:50:40
1. reference:参考某一资源id。(1)属性定义:复制代码 代码如下: &nb...
1. reference:参考某一资源id。
(1)属性定义:
<declare-styleable name = "名称">
<attr name = "background" format = "reference" />
</declare-styleable>
(2)属性使用:
<imageview
android:layout_width = "42dip"
android:layout_height = "42dip"
android:background = "@drawable/图片id"
/>
2. color:颜色值。
(1)属性定义:
<declare-styleable name = "名称">
<attr name = "textcolor" format = "color" />
</declare-styleable>
(2)属性使用:
<textview
android:layout_width = "42dip"
android:layout_height = "42dip"
android:textcolor = "#00ff00"
/>
3. boolean:布尔值。
(1)属性定义:
<declare-styleable name = "名称">
<attr name = "focusable" format = "boolean" />
</declare-styleable>
(2)属性使用:
<button
android:layout_width = "42dip"
android:layout_height = "42dip"
android:focusable = "true"
/>
4. dimension:尺寸值。
(1)属性定义:
<declare-styleable name = "名称">
<attr name = "layout_width" format = "dimension" />
</declare-styleable>
(2)属性使用:
<button
android:layout_width = "42dip"
android:layout_height = "42dip"
/>
5. float:浮点值。
(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:整型值。
(1)属性定义:
<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:字符串。
(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 = "0jokq80od1jl9c6haja99ugxcris2cgjko_bc_g"
/>
8. fraction:百分数。
(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:枚举值。
(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:位或运算。
(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>
注意:
属性定义时可以指定多种类型值。
(1)属性定义:
<declare-styleable name = "名称">
<attr name = "background" format = "reference|color" />
</declare-styleable>
(2)属性使用:
<imageview
android:layout_width = "42dip"
android:layout_height = "42dip"
android:background = "@drawable/图片id|#00ff00"
/>
(1)属性定义:
复制代码 代码如下:
<declare-styleable name = "名称">
<attr name = "background" format = "reference" />
</declare-styleable>
(2)属性使用:
复制代码 代码如下:
<imageview
android:layout_width = "42dip"
android:layout_height = "42dip"
android:background = "@drawable/图片id"
/>
2. color:颜色值。
(1)属性定义:
复制代码 代码如下:
<declare-styleable name = "名称">
<attr name = "textcolor" format = "color" />
</declare-styleable>
(2)属性使用:
复制代码 代码如下:
<textview
android:layout_width = "42dip"
android:layout_height = "42dip"
android:textcolor = "#00ff00"
/>
3. boolean:布尔值。
(1)属性定义:
复制代码 代码如下:
<declare-styleable name = "名称">
<attr name = "focusable" format = "boolean" />
</declare-styleable>
(2)属性使用:
复制代码 代码如下:
<button
android:layout_width = "42dip"
android:layout_height = "42dip"
android:focusable = "true"
/>
4. dimension:尺寸值。
(1)属性定义:
复制代码 代码如下:
<declare-styleable name = "名称">
<attr name = "layout_width" format = "dimension" />
</declare-styleable>
(2)属性使用:
复制代码 代码如下:
<button
android:layout_width = "42dip"
android:layout_height = "42dip"
/>
5. float:浮点值。
(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:整型值。
(1)属性定义:
复制代码 代码如下:
<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:字符串。
(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 = "0jokq80od1jl9c6haja99ugxcris2cgjko_bc_g"
/>
8. fraction:百分数。
(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:枚举值。
(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:位或运算。
(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>
注意:
属性定义时可以指定多种类型值。
(1)属性定义:
复制代码 代码如下:
<declare-styleable name = "名称">
<attr name = "background" format = "reference|color" />
</declare-styleable>
(2)属性使用:
复制代码 代码如下:
<imageview
android:layout_width = "42dip"
android:layout_height = "42dip"
android:background = "@drawable/图片id|#00ff00"
/>