标签:integer out dimens 色值 颜色 strong 自己的 尺寸 com
写个自定义控件时经常要自定义一些自己的属性,平时用的都是那几个,今天就顺便一起总结一下这个东东吧~
一、定义:属性的定义都在attrs.xml文件里面;
二、读取:通过都是通过TypedArray去读取的,要获取TypedArray都是通过context.obtainStyledAttributes去获取的,它有几个重载方法,一般形如: TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomView);
  三、使用:要使用自定义属性,得先在布局文件声明 xmlns:app="http://schemas.android.com/apk/res-auto" 当然,你不喜欢app也可以自定义名字,形如:xmlns:custom="http://schemas.android.com/apk/res/{packagename}" 。
四、自定义format的概览:
| format名称 | format类型 | 
| reference | 
 | 
| string | 
 | 
| color | 
 | 
| boolean | 
 | 
| dimension | 
 | 
| float | 
表示浮点值  | 
| integer | 
 | 
| fraction | 
 | 
| enum | 
 | 
| flag | 
 | 
五、具体说明:
5.1. reference:参考某一资源ID。
(1)属性定义:
<declare-styleable name = "名称">
<attr name = "cutom_id" format = "reference" />
</declare-styleable>
(2)属性使用:
<CustomView
                     android:layout_width = "wrap_content"
                     android:layout_height = "wrap_content"
                     app:cutom_id = "@drawable/图片ID"
/>
5.2. color:颜色值。
(1)属性定义:
<declare-styleable name = "名称">
<attr name = "custom_color" format = "color" />
</declare-styleable>
(2)属性使用:
<CustomView
                     android:layout_width = "wrap_content"
                     android:layout_height = "wrap_content"
                     app:custom_color = "#00FF00"
/>
5.3. boolean:布尔值。
(1)属性定义:
<declare-styleable name = "名称">
<attr name = "custom_b" format = "boolean" />
</declare-styleable>
(2)属性使用:
<CustomView
                    android:layout_width = "wrap_content"
                    android:layout_height = "wrap_content"
app:custom_b = "true"
/>
5.4. dimension:尺寸值。
(1)属性定义:
<declare-styleable name = "名称">
<attr name = "custom_width" format = "dimension" />
</declare-styleable>
(2)属性使用:
<CustomView
                    android:layout_width = "wrap_content"
                    android:layout_height = "wrap_content"
app:custom_width="44dp"
/>
5.5. float:浮点值。
(1)属性定义:
<declare-styleable name = "名称">
<attr name = "custom_alpha" format = "float" />
</declare-styleable>
(2)属性使用:
            <CustomView
              android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
app:custom_alpha="0.5"
/>
5.6. integer:整型值。
(1)属性定义:
<declare-styleable name = "名称">
<attr name = "custom_number" format="integer" />
</declare-styleable>
(2)属性使用:
<CustomView
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
app:custom_number="5"
/>
5.7. string:字符串。
(1)属性定义:
            <declare-styleable name = "名称">
                   <attr name = "custom_key" format = "string" />
            </declare-styleable>
(2)属性使用:
            <CustomView
                    android:layout_width = "wrap_content"
                    android:layout_height = "wrap_content"
                    app:custom_key = "test_msg"
/>
5.8. fraction:百分数。
(1)属性定义:
            <declare-styleable name="名称">
                   <attr name = "custom_percent" format = "fraction" />
            </declare-styleable>
(2)属性使用:
<CustomView
                 android:layout_width = "wrap_content"
                 android:layout_height = "wrap_content"
app:custom_percent = "200%"
/>
5.9. enum:枚举值。
(1)属性定义:
            <declare-styleable name="名称">
                   <attr name="custom_orientation">
                          <enum name="horizontal" value="0" />
                          <enum name="vertical" value="1" />
                   </attr>            
</declare-styleable>
(2)属性使用:
<CustomView
                    android:layout_width = "wrap_content"
                    android:layout_height = "wrap_content"
                    app:custom_orientation = "vertical"
            />
5.10. flag:位或运算。
(1)属性定义:
             <declare-styleable name="名称">
                    <attr name="custom_mode">
                            <flag name = "mode_one" value = "0" />
                            <flag name = "mode_two" value = "1" />
                            <flag name = "mode_three" value = "2" />
                     </attr>         
</declare-styleable>
(2)属性使用:
<CustomView
                    android:layout_width = "wrap_content"
                    android:layout_height = "wrap_content"
                    app:custom_mode = "mode_one|mode_two|mode_three"
            />
5.11 注意: 属性定义时可以指定多种类型值。
(1)属性定义:
<declare-styleable name = "名称">
<attr name = "custom_background" format = "reference|color" />
</declare-styleable>
(2)属性使用:
<CustomView
                     android:layout_width = "wrap_content"
                     android:layout_height = "wrap_content"
                     app:custom_background = "@drawable/图片ID|#00FF00"
/>
标签:integer out dimens 色值 颜色 strong 自己的 尺寸 com
原文地址:http://www.cnblogs.com/wytings/p/6974217.html