在res资源目录的values目录下创建一个attrs.xml的属性定义文件,并在该文件中通过如下代码定义相应的属性即可。
属性名,属性格式<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="instruction"> <attr name="focusable" format="boolean" /> <attr name="layout_width" format="dimension" /> <attr name="pointX" format="float" /> <attr name="rightTextColor" format="color" /> <attr name="rightBackground" format="reference|color" />引用类型或者color型 <attr name="pointY" format="integer" /> <attr name="text" format="string" /> <attr name="pivotX" format="fraction" /> </declare-styleable> 枚举值 <declare-styleable name="enum"> <attr name="myEnum"> <enum name="horizontal" value="0" /> <enum name="vertical" value="1" /> </attr> </declare-styleable> 位或运算 <declare-styleable name="flag"> <attr name="myFlag"> <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" /> </attr> </declare-styleable> </resources>
使用方法:attr name名+类型
示范一:定义 <declare-styleable name = "color"> <attr name = "background" format = "reference|color" /> </declare-styleable> 使用 <ImageView android:background = "@drawable/图片ID|#00FF00" /> 示范二: 定义 <declare-styleable name="orient"> <attr name="orientation"> <enum name="horizontal" value="0" /> <enum name="vertical" value="1" /> </attr> </declare-styleable> 使用<LinearLayout android:orientation = "horizontal" /> 示范三:定义<declare-styleable name="name"> <attr name="windowSoftInputMode"> <flag name="stateUnspecified" value="0" /> <flag name="stateUnchanged" value="1" /> <flag name="stateHidden" value="2" /> </attr></declare-styleable>使用<activity andriod:windowsSotfInputMode = "stateUpspecified | stateUnchanged"/>
时间: 2024-10-25 07:16:00