Custom controls are often used in recent projects, so custom attributes are often used, and here is a description of the use of custom attributes:
Custom attributes exist in the/value/attr.xml file and exist in the following format.
Copy Code code as follows:
<resource>
<declare-styleable name= "Custom property name" >
<attr name= "Property name" format= "Property category"/>
......
</declare-styleable>
</resource>
The value for the format in the custom attribute and its meaning are as follows:
Format property values: Reference, Color, Boolean, dimension, Float, Integer, String, fraction, enum, flag
1. Reference: Refer to a resource ID.
(1) Attribute definition:
Copy Code code as follows:
<declare-styleable name = "Name" >
<attr name = "Background" format = "Reference"/>
</declare-styleable>
(2) Property use:
Copy Code code as follows:
<imageview
Android:layout_width= "42dip"
android:layout_height= "42dip"
android:background= "@drawable/Picture ID"
/>
2.color: Color value.
(1) Attribute definition:
Copy Code code as follows:
<declare-styleablename= "Name" >
<attrname= "TextColor" format= "Color"/>
</declare-styleable>
(2) Property use:
Copy Code code as follows:
<textview
Android:layout_width= "42dip"
android:layout_height= "42dip"
Android:textcolor= "#00FF00"
/>
3.boolean: Boolean value.
(1) Attribute definition:
Copy Code code as follows:
<declare-styleablename= "Name" >
<attrname= "Focusable" format= "boolean"/>
</declare-styleable>
(2) Property use:
Copy Code code as follows:
<button
Android:layout_width= "42dip"
android:layout_height= "42dip"
Android:focusable= "true"
/>
4.dimension: Dimension value.
(1) Attribute definition:
Copy Code code as follows:
<declare-styleablename= "Name" >
<attrname= "Layout_width" format= "Dimension"/>
</declare-styleable>
(2) Property use:
Copy Code code as follows:
<button
Android:layout_width= "42dip"
android:layout_height= "42dip"
/>
5.float: Floating-point value.
(1) Attribute definition:
Copy Code code as follows:
<declare-styleablename= "Alphaanimation" >
<attrname= "Fromalpha" format= "float"/>
<attrname= "Toalpha" format= "float"/>
</declare-styleable>
(2) Property use:
Copy Code code as follows:
<alpha
Android:fromalpha= "1.0"
Android:toalpha= "0.7"
/>
6.integer: integer value.
(1) Attribute definition:
Copy Code code as follows:
<declare-styleablename= "Animatedrotatedrawable" >
<attrname= "Visible"/>
<attrname= "frameduration" format= "integer"/>
<attrname= "Framescount" format= "integer"/>
<attrname= "Pivotx"/>
<attrname= "Pivoty"/>
<attrname= "drawable"/>
</declare-styleable>
(2) Property use:
Copy Code code as follows:
<animated-rotate
Xmlns:android= "Http://schemas.android.com/apk/res/android"
android:drawable= "@drawable/Picture ID"
android:pivotx= "50%"
Android:pivoty= "50%"
Android:framescount= "12"
android:frameduration= "100"
/>
7.string: String.
(1) Attribute definition:
Copy Code code as follows:
<declare-styleablename= "Mapview" >
<attrname= "Apikey" format= "string"/>
</declare-styleable>
(2) Property use:
Copy Code code as follows:
<com.google.android.maps.mapview
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:apikey= "0jokq80od1jl9c6haja99ugxcris2cgjko_bc_g"
/>
8.fraction: Percent.
(1) Attribute definition:
Copy Code code as follows:
<declare-styleablename= "Rotatedrawable" >
<attrname= "Visible"/>
<attrname= "Fromdegrees" format= "float"/>
<attrname= "Todegrees" format= "float"/>
<attrname= "Pivotx" format= "fraction"/>
<attrname= "Pivoty" format= "fraction"/>
<attrname= "drawable"/>
</declare-styleable>
(2) Property use:
Copy Code code as follows:
<rotate
Xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:interpolator= "@anim/Animation ID"
android:fromdegrees= "0"
Android:todegrees= "360"
android:pivotx= "200%"
Android:pivoty= "300%"
android:duration= "5000"
Android:repeatmode= "Restart"
Android:repeatcount= "Infinite"
/>
9.enum: enumeration value.
(1) Attribute definition:
Copy Code code as follows:
<declare-styleablename= "Name" >
<attrname= "Orientation" >
<enumname= "Horizontal" value= "0"/>
<enumname= "Vertical" value= "1"/>
</attr>
</declare-styleable>
(2) Property use:
Copy Code code as follows:
<linearlayout
Xmlns:android= "Http://schemas.android.com/apk/res/android"
android:orientation= "Vertical"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent" >
</LinearLayout>
10.flag: Bit or operation.
(1) Attribute definition:
Copy Code code as follows:
<declare-styleablename= "Name" >
<attrname= "Windowsoftinputmode" >
<flagname= "stateunspecified" value= "0"/>
<flagname= "Stateunchanged" value= "1"/>
<flagname= "Statehidden" value= "2"/>
<flagname= "Statealwayshidden" value= "3"/>
<flagname= "Statevisible" value= "4"/>
<flagname= "Statealwaysvisible" value= "5"/>
<flagname= "adjustunspecified" value= "0x00"/>
<flagname= "Adjustresize" value= "0x10"/>
<flagname= "Adjustpan" value= "0x20"/>
<flagname= "adjustnothing" value= "0x30"/>
</attr>
</declare-styleable>
(2) Property use:
Copy Code code as follows:
<activity
Android:name= ". Styleandthemeactivity "
Android:label= "@string/app_name"
Android:windowsoftinputmode= "Stateunspecified|stateunchanged | Statehidden ">
<intent-filter>
<actionandroid:name= "Android.intent.action.MAIN"/>
<categoryandroid:name= "Android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
Special attention should be paid to:
You can specify multiple type values when you define a property.
(1) Attribute definition:
Copy Code code as follows:
<declare-styleablename= "Name" >
<attrname= "Background" format= "Reference|color"/>
</declare-styleable>
(2) Property use:
Copy Code code as follows:
<imageview
Android:layout_width= "42dip"
android:layout_height= "42dip"
android:background= "@drawable/Picture id| #00FF00"
/>
Here's how AttributeSet and Typedarray work in custom controls:
The role of AttributeSet is to encapsulate the control's properties (Keyeg:background) and the value (Valueeg: @drawable/icon) in the layout file in AttributeSet when the control initializes, The constructor that is passed to the control (View). For non-Android self-contained attributes, it is not recognized in the view class, so we need to parse it ourselves. So this is going to use another class Typedarray. In AttributeSet We have attribute names, property values, but how does the control know which attribute represents? The work was done by Typedarray. The Typedarray object encapsulates the/values/ The type information of each attribute defined in the styleable in the Attrs.xml, by Typedarray we can know what the value of the encapsulated values in AttributeSet is doing, so that the data can be applied.
AttributeSet is equivalent to a box of sugar, Typedarray is equivalent to this box of sugar on the label, tell the user the taste of each sugar. The Taste of this box of sugar is determined by the contents of the user's own styleable file.