@(Android技术文章)
个人博客地址: 斯科特安的时间
在Android项目的实际开发中,免不了要自定义一些控件或者view,通过 xml 自定义 view 属性是提高代码质量的一大关键要素,这里对自定义属性做一记录。
1. reference:参考某一资源ID
属性定义:
<declare-styleable name="名称">
<attr name="background" format="reference" />
</declare-styleable>
2. color:颜色值
<declare-styleable name="名称">
<attr name="textColor" format="color" />
</declare-styleable>
3. boolean:布尔值
<declare-styleable name="名称">
<attr name="focusable" format="boolean" />
</declare-styleable>
4. dimension:尺寸值
<declare-styleable name="名称">
<attr name="layout_width" format="dimension" />
</declare-styleable>
5. float:浮点值
<declare-styleable name="AlphaAnimation">
<attr name="fromAlpha" format="float" />
<attr name="toAlpha" format="float" />
</declare-styleable>
6. integer:整型值
<declare-styleable name="AnimatedRotateDrawable">
<attr name="frameDuration" format="integer" />
<attr name="framesCount" format="integer" />
</declare-styleable>
7. string:字符串
<declare-styleable name="MapView">
<attr name="apiKey" format="string" />
</declare-styleable>
8. fraction:百分数
<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>
9. enum:枚举值
<declare-styleable name="名称">
<attr name="orientation">
<enum name="horizontal" value="0" />
<enum name="vertical" value="1" />
</attr>
</declare-styleable>
10. flag:位或运算
<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>
注意
属性定义时可以指定多种类型值,如:
属性定义:
<declare-styleable name="名称">
<attr name="background" format="reference|color" />
</declare-styleable>