Android supports custom controls. Sometimes we need to add some attributes to these custom spaces.CodeThere is no problem in defining attribute variables. How to use attributes in XML files? You need to customize the control attributes in the XML file. (You can refer to D: \ Android-SDK \ platforms \ Android-16 \ data \ res \ values \ attrs. XML, which is only a file defined by the attribute of the Android system)
Assume that we have written a commontitle custom control
Public class commontitle extends linearlayout {private color textcolor; private float textsize; private string text ;}
This commontitle has three attributes: textcolor, textsize, and text.
When I use this commontitle in the layout file, how do I set values for these three attributes? You may think of Android: textcolor, but this is a control we write, so it is useless to set Android: textcolor. Can you define a property by yourself? For example, myandroid: textcolor. The answer is yes!
1. To define custom control properties, you must define an attrs. xml file in the Res/values file. The Code is as follows:
<? XML version = "1.0" encoding = "UTF-8"?> <Resources> <declare-styleable name = "commontitle"> <ATTR name = "text" format = "string"> </ATTR> <ATTR name = "textcolor" format =" color "> </ATTR> <ATTR name =" textsize "format =" dimension "> </ATTR> </declare-styleable> </resources>
In this way, three control attributes are customized.
2. Use this attribute in layout XML as follows:
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" xmlns: myandroid = "http://schemas.android.com/apk/res/com.tianjf" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <COM. tianjf. view. commontitle Android: layout_width = "fill_parent" Android: layout_height = "50dip" myandroid: text = "@ string/str_weightlist_title" myandroid: textcolor = "@ Android: color/darker_gray" myandroid: textsize = "@ dimen/weight_list_title_text_size"/> </linearlayout>
3. In custom components, you can get the values defined in XML as follows:
Public commontitle (context, attributeset attrs) {super (context, attrs); mcontext = context; typedarray A = context. obtainstyledattributes (attrs, R. styleable. commontitle); mtext =. getstring (R. styleable. commontitle_text); mtextcolor =. getcolor (R. styleable. commontitle_textcolor, 0 xffffffff); mtextsize =. getdimensionpixelsize (R. styleable. commontitle_textsize, 0);. recycle ();}
The preceding three steps allow you to create and use custom attributes.
First, let's take a look at the attrs. xml file.
The name in <declare-styleable name = "commontitle"> is used in Java code. See step 3.
The formart attribute can have the following types:
String, integer, dimension, reference, color, enum.
The preceding methods are consistent, for example, <ATTR name = "buttonnum" format = "integer"/>.
Only Enum is different. For example:
<ATTR name = "testenum"> <Enum name = "fill_parent" value = "-1"/> <Enum name = "wrap_content" value = "-2"/> </ ATTR>
If two different attributes can be uploaded at the same time, you can split them with "|.
Let's take a look at the precautions in the layout XML.
First declare: xmlns: myandroid = http://schemas.android.com/apk/res/com.tianjf
Note: "myandroid" can be replaced with any other name, and the subsequent URL address must use the package name of the custom component in the last part. After customizing attributes, add "myandroid" before the attribute name.
Finally, let's take a look at the precautions in Java code.
In the constructor of a custom component, use
Typedarray A = context. obtainstyledattributes (attrs, R. styleable. commontitle );
To obtain the reference to the attribute set, and then you can use the "A" method to obtain the corresponding attribute value. Note that if the method used and the type of the obtained value are incorrect, the default value is returned. Therefore, if an attribute contains two or more non-essential attributes, you must make multiple judgments to determine the value to be assigned after reading the attribute. Of course, don't forget to recycle resources when you get the value!