Write a custom control often want to customize some of their own properties, usually used are those few, today by the way together to summarize this stuff ~
Definition: The definition of a property is in the Attrs.xml file;
Second, read: Through all through the TypedArray to read, to obtain TypedArray are obtained through context.obtainstyledattributes, it has several overloaded methods, general shape such as: TypedArray TypedArray = Context.obtainstyledattributes (Attrs, R.styleable.customview);
Third, use: To use a custom attribute, the layout file must first declare xmlns:app= "Http://schemas.android.com/apk/res-auto" Of course, you do not like the app can also be customized names, such as: xmlns:custom="http://schemas.android.com/apk/res/{packagename}" .
Iv. Overview of custom format:
| Format name |
Format type |
| Reference |
表示引用,参考某一资源ID
|
| String |
表示字符串
|
| Color |
表示颜色值
|
| Boolean |
表示尺寸值
|
| Dimension |
表示布尔值
|
| Float |
Represents a floating-point value |
| Integer |
表示整型值
|
| Fraction |
表示百分数
|
| Enum |
表示枚举值
|
| Flag |
表示位运算
|
V. Specific Description:
5.1. Reference: Refer to a resource ID.
(1) Attribute definition:
<declare-styleable name = "Names" >
<attr name = "cutom_id" format = "Reference"/>
</declare-styleable>
(2) attribute use:
<customview
Android:layout_width = "Wrap_content"
Android:layout_height = "Wrap_content"
app:cutom_id = "@drawable/Picture ID"
/>
5.2. Color: Colors value.
(1) Attribute definition:
<declare-styleable name = "Names" >
<attr name = "Custom_color" format = "Color"/>
</declare-styleable>
(2) attribute use:
<customview
Android:layout_width = "Wrap_content"
Android:layout_height = "Wrap_content"
App:custom_color = "#00FF00"
/>
5.3. Boolean: Boolean value.
(1) Attribute definition:
<declare-styleable name = "Names" >
<attr name = "Custom_b" format = "Boolean"/>
</declare-styleable>
(2) attribute use:
<customview
Android:layout_width = "Wrap_content"
Android:layout_height = "Wrap_content"
App:custom_b = "true"
/>
5.4. Dimension: Dimension value.
(1) Attribute definition:
<declare-styleable name = "Names" >
<attr name = "custom_width" format = "Dimension"/>
</declare-styleable>
(2) attribute use:
<customview
Android:layout_width = "Wrap_content"
Android:layout_height = "Wrap_content"
App:custom_width= "44DP"
/>
5.5. Float: floating-point value.
(1) Attribute definition:
<declare-styleable name = "Names" >
<attr name = "Custom_alpha" format = "float"/>
</declare-styleable>
(2) attribute use:
<customview
Android:layout_width = "Wrap_content"
Android:layout_height = "Wrap_content"
App:custom_alpha= "0.5"
/>
5.6. Integer: Integer value.
(1) Attribute definition:
<declare-styleable name = "Names" >
<attr name = "Custom_number" format= "integer"/>
</declare-styleable>
(2) attribute use:
<customview
Android:layout_width = "Wrap_content"
Android:layout_height = "Wrap_content"
App:custom_number= "5"
/>
5.7. String: String.
(1) Attribute definition:
<declare-styleable name = "Names" >
<attr name = "Custom_key" format = "string"/>
</declare-styleable>
(2) attribute use:
<customview
Android:layout_width = "Wrap_content"
Android:layout_height = "Wrap_content"
App:custom_key = "Test_msg"
/>
5.8. Fraction: percentage.
(1) Attribute definition:
<declare-styleable name= "Name" >
<attr name = "custom_percent" format = "fraction"/>
</declare-styleable>
(2) attribute use:
<customview
Android:layout_width = "Wrap_content"
Android:layout_height = "Wrap_content"
App:custom_percent = "200%"
/>
5.9. Enum: enumeration value.
(1) Attribute definition:
<declare-styleable name= "Name" >
<attr name= "Custom_orientation" >
<enum name= "Horizontal" value= "0"/>
<enum name= "Vertical" value= "1"/>
</attr>
</declare-styleable>
(2) attribute use:
<customview
Android:layout_width = "Wrap_content"
Android:layout_height = "Wrap_content"
App:custom_orientation = "vertical"
/>
5.10. Flag: Bit or operation.
(1) Attribute definition:
<declare-styleable name= "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) attribute use:
<customview
Android:layout_width = "Wrap_content"
Android:layout_height = "Wrap_content"
App:custom_mode = "Mode_one|mode_two|mode_three"
/>
5.11 Note: You can specify multiple types of values when you define a property.
(1) Attribute definition:
<declare-styleable name = "Names" >
<attr name = "Custom_background" format = "Reference|color"/>
</declare-styleable>
(2) attribute use:
<customview
Android:layout_width = "Wrap_content"
Android:layout_height = "Wrap_content"
App:custom_background = "@drawable/Picture id| #00FF00"
/>
Description of the Android Attrs custom properties