Http://www.cnblogs.com/jisheng/archive/2013/01/10/2854891.html
In the process of use,
1 TypedArray a = getcontext (). Obtainstyledattributes (Attrs, r.styleable.contactlistitemview); 2 Mpreferredheight = a.getdimensionpixelsize (3 r.styleable.contactlistitemview_list_item_height, 0); 4 mactivatedbackgrounddrawable = a.getdrawable (r.styleable.contactlistitemview_activated_background); 6 mhorizontaldividerdrawable = a.getdrawable (7 r.styleable.contactlistitemview_list_item_divider);
Found that the mhorizontaldividerdrawable has been empty.
Found in the Attrs.xml defines the
<name= "Contactlistitemview">.... <name= "List_item_divider" format= "Reference"/>... </declare-styleable>
Finally found that the reference appeared in Activitytheme
<name= "Peopletheme" Parent= "@android: Style/theme.holo.light.darkactionbar"> ....... <name= "List_item_divider">?android:attr/listdivider</Item> ....... </style>
The relevant information is as follows
Doing the Android layout is a pleasure, thanks to his good XML approach. Using XML can quickly and efficiently define the interface for the software. But sometimes we feel that some of the basic components of the official definition are not enough, and custom components are unavoidable. So how do you define his attributes in XML like those that are officially provided? Now let's talk about his usage.
First, define a attrs.xml file under the Res/values file with the following code:
<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
<declare-styleable name= "ToolBar" >
<attr name= "Buttonnum" format= "integer"/>
<attr name= "Itembackground" format= "Reference|color"/>
</declare-styleable>
</resources>
Second, use this property in the layout XML as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:toolbar= "Http://schemas.android.com/apk/res/cn.zzm.toolbar"
android:orientation= "Vertical"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
>
<cn.zzm.toolbar.toolbar android:id= "@+id/gridview_toolbar"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
Android:layout_alignparentbottom= "true"
android:background= "@drawable/control_bar"
android:gravity= "Center"
Toolbar:buttonnum= "5"
toolbar:itembackground= "@drawable/control_bar_item_bg"/>
</RelativeLayout>
Third, in the custom component, you can get the values defined in the XML as follows:
TypedArray a = context.obtainstyledattributes (Attrs,r.styleable.toolbar);
Buttonnum = A.getint (R.styleable.toolbar_buttonnum, 5);
ITEMBG = A.getresourceid (R.styleable.toolbar_itembackground,-1);
A.recycle ();
In this simple three-step way, you can complete the use of custom attributes.
*********************************************************************
OK, the basic usage is finished, now look at some points of attention and knowledge points.
First look at the Attrs.xml file.
This file is where you define the property name and format, and you need to <declare-styleable name= "ToolBar" ></declare-styleable> surround all the attributes. Where name is the name of the property set, the primary purpose is to identify the property set. Where is it going to be used? Mainly in the third step. Did you see that? When getting a property ID, "R.styleable.toolbar_buttonnum" is used, and it is clear that he adds "toolbar_" to each property.
Look at the types of properties: String, Integer, dimension, reference, color, enum ...
The preceding declarations are consistent, for example: <attr name= "Buttonnum" format= "integer"/>.
Only the enum is different, use an example:
<attr name= "Testenum" >
<enum name= "Fill_parent" value= "-1"/>
<enum name= "Wrap_content" value= "-2"/>
</attr>
If the property can pass two different properties at the same time, you can use the ' | ' Split open can.
Let's take a look at the considerations in the layout XML.
The first thing to declare: Xmlns:toolbar=http://schemas.android.com/apk/res/cn.zzm.toolbar
Note that the "toolbar" can be replaced by any other name, and the URL address must be the last part of the package name of the custom component. Custom attribute, precede the attribute name with "toolbar".
Finally, take a look at the considerations in Java code.
In the constructor of the custom component, use the
TypedArray a = context.obtainstyledattributes (Attrs,r.styleable.toolbar);
To get a reference to the property set, and then you can use the various methods of "a" to get the corresponding property value. It is important to note that the default value is returned if the method used and the type of the obtained value are incorrect. Therefore, if a property is a property with two or more unused types, it is necessary to make multiple judgments, knowing that the value should be given when the read is complete. Of course, don't forget to recycle the resources when you have finished the value!
Introduction to custom Attribute data types:
First, reference: Refer to the resource ID in the specified theme.
1. Definition:
123 |
<declare-styleable name= "My"> <attr> </ Declare-styleable> |
2. Use:
Second, color: colors
1. Definition:
123 |
<declare-styleable name= "My"> <attr/> </declare-styleable> |
2. Use:
1 |
<button zkx:textcolor= "#ff0000"/> |
Three, Boolean: Boolean value
1. Definition:
123 |
<declare-styleable name= "My"> <attr/> </ Declare-styleable> |
1. Function:
1 |
<button zkx:isvisible= "false"/> |
Iv. Dimension: Dimension value
1. Definition:
123 |
<declare-styleable name= "My"> <attr/> </ Declare-styleable> |
1. Function:
1 |
<button zkx:mywidth= "100dip"/> |
V. Float: Float type
1. Definition:
123 |
<declare-styleable name= "My"> <attr/> </declare-styleable> |
1. Function:
1 |
<alpha zkx:fromalpha= "0.3"/> |
Six, integer: integer type
1. Definition:
123 |
<declare-styleable name= "My"> <attr/> </ Declare-styleable> |
2. Use:
1 |
<animated-rotate zkx:framescount= "/> " |
Seven, String: strings
1. Definition:
123 |
<declare-styleable name= "My"> <attr/> </ Declare-styleable> |
2. Use:
1 |
<rotate zkx:pivotx= "200%"/> |
Viii. Fraction: Percentage
1. Definition:
123 |
<declare-styleable name= "My"> <attr/> </declare-styleable> |
2. Use:
1 |
<rotate zkx:name= "My Name is Zhang Kun xiang"/> |
IX. Enum: Enumeration
1. Definition:
12345 |
<declare-styleable name= "My"> <attr name= "language"> <enum name= "中文版" value= "1"/> </attr> </declare-styleable> |
2. Use:
1 |
<button zkx:language= "中文版"/> |
X. FLAG: Bitwise OR operation
1. Definition:
123456 |
<declare-styleable name= "My"> <attr name= "Windowsoftinputmode"> <flag/> <flag/> </attr> </declare-styleable> |
2. Use:
1 |
<activity android:windowsoftinputmode= "stateunspecified | Adjustnothing "> |
Multiple types of values can be specified when the property is defined:
123 |
<declare-styleable name = "Name"> <attr/> </ Declare-styleable> |
Use:
1 |
<imageview android:background = "@drawable/Picture id| #00FF00"/> |
Declare-styleable: Properties of custom controls