Android 自定义控件☞添加attr属性

来源:互联网 发布:python安装包怎么下载 编辑:程序博客网 时间:2024/06/05 04:51

在attr中添加自定义的属性时,如果该属性非android空间已有的属性,必须加format声明,否则在解析时,会报错。

<resources>

<!-- 引用android已有属性,不用指定format-- >
    <declare-styleable name="CustomImageButton">
        <attr name="android:src"/>
        <attr name="android:text"/>
        <attr name="android:textSize"/>
    </declare-styleable>
<!-- 完全自定义属性,需要指定format-- >
    <declare-styleable name="SlipButton">
        <attr name="onBg" format="reference"/>
        <attr name="offBg" format="reference"/>
        <attr name="slipImg" format="reference"/>
    </declare-styleable>
</resources>


  注1:format可选项 
  "reference" //引用 
  "color" //颜色 
  "boolean" //布尔值 
  "dimension" //尺寸值 
  "float" //浮点值 
  "integer" //整型值 
  "string" //字符串 
  "fraction" //百分数,比如200% 
  枚举值,格式如下: 
  < attr name="orientation"> 
  < enum name="horizontal" value="0" /> 
  < enum name="vertical" value="1" /> 
  < /attr> 
  xml中使用时: 
  android:orientation = "vertical" 
  标志位,位或运算,格式如下: 
  < 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> 
  xml中使用时: 
  android:windowSoftInputMode = "stateUnspecified | stateUnchanged | stateHidden"> 
  另外属性定义时可以指定多种类型值,比如: 
  < attr name = "background" format = "reference|color" /> 
  xml中使用时: 
  android:background = "@drawable/图片ID|#00FF00"
 
原创粉丝点击