Sometimes it is felt that the controls provided in Android do not meet the requirements of the project, so they are often used to customize the controls. Custom controls will inevitably customize the properties. Custom attributes take approximately three steps : Define the name and data type of the custom attribute in the XML file , invoke the custom property in the layout, and get the custom property in the code. Here's a detailed explanation of these three steps.
First, define the name and data type of the custom attribute in the XML file
Create a new Attrs.xml file under the project's Res/values folder and set the name and type of the custom attribute in the file. The code is as follows:
1 <?XML version= "1.0" encoding= "Utf-8"?> 2 <Resources> 3 <declare-styleablename= "ToolBar"> 4 <attrname= "Buttonnum"format= "integer"/> 5 <attrname= "Itembackground"format= "Reference|color"/> 6 </declare-styleable> 7 </Resources>
code in the Attrs.xml file
Here are a few things to keep in mind:
1, this file is to define the property name and attribute data format place, need to use <declare-styleable name= "..." ><declare-styleable/> label wrap all custom attributes. In this tag, name is the name of the property set, and the primary purpose is to identify the property set. A project can have multiple custom controls, but only one attrs.xml file, so we need to use a label to differentiate the set of properties for each custom control. In addition to the role of the control, this name is also used in the third step of the following, that is, in the Java code to use this name to remove this property.
2, under the <declare-styleable> label, we need to use <attr name= "..." format= "..." ></attr> to define properties and their data types. The main data types are the following: string (String), Integer (shape, such as 12), float (float, such as 0.7), dimension (size, such as 10.0dip), Boolean (Boolean, such as true), reference ( Refer to a resource ID, such as @string/app_name), color (color code, such as #ff0000), fraction (percent, such as 200%), enum (enum).
Of the above types, we have two points to note:
(1) The definition of enum enum type is different from other types of data, and the definition instance of enum type data is shown in the following code:
1 <declare-styleablename= "ToolBar">2 <attrname= "Orientation">3 <enumname= "Horizontal"value= "0" />4 <enumname= "vertical"value= "1" />5 </attr>6 </declare-styleable>
enumeration Type Property Definition
(2) These properties can be combined to use, like <attr name= "background" format= " Reference|color " /> This means that you can pass a number of different values, that is, the background property can be either a defined value or a ready-made color.
Ii. calling custom properties in a layout file
The code for using a custom property in a layout file is as follows:
1 <?XML version= "1.0" encoding= "Utf-8"?> 2 <Relativelayout3 xmlns:android= "Http://schemas.android.com/apk/res/android" 4 Xmlns:toolbar= "Http://schemas.android.com/apk/res/cn.zzm.toolbar" 5 Android:layout_width= "Fill_parent" 6 Android:layout_height= "Fill_parent" 7 > 8 <Cn.zzm.toolbar.ToolBarAndroid:id= "@+id/gridview_toolbar" 9 Android:layout_width= "Fill_parent" Ten Android:layout_height= "Wrap_content" One Android:layout_alignparentbottom= "true" A Android:background= "@drawable/control_bar" - android:gravity= "Center" - Toolbar:buttonnum= "5" the Toolbar:itembackground= "@drawable/control_bar_item_bg"/> - </Relativelayout>
code for the layout file
This step is to write the custom attributes defined in the Attrs.xml file to the layout file in the first step. Here we still need to pay attention to a few questions:
1, we need to first declare these custom attributes of the namespace (NameSpace). The Android-based properties we usually use will start with "Android:" So we'll see this line of code in the first few lines of each layout file: xmlns:android= "http://schemas.android.com/apk/ Res/android " , which means declaring a namespace in which all properties in this namespace begin with" Android: ". Therefore, if we need to use our own defined properties, we also need to define such a similar namespace at the beginning of the file. In the above code, we define namespaces like this: xmlns:toolbar= "Http://schemas.android.com/apk/res/cn.zzm.toolbar" , This means that the name of our custom namespace is called toolbar, and we need to precede the "toolbar:" prefix when calling custom attributes.
2, in our custom namespace has such a piece of code: "Cn.zzm.toolbar", many people think this is the package name of the custom control, actually not, here is the main interface of the Java file is the package name, we can go to the project's Menifest file (< The value in the package attribute in the menifest> tag) to view and copy.
3, the name of the namespace is unlimited, we can be arbitrarily formulated, there is no specific provisions, such as the above code in the toolbar.
4. When using custom attributes in custom controls, we need to find this property through the namespaces we have defined above, such as: toolbar:buttonnum= "5" .
Iii. getting the value of a custom property in code
This is typically done in the construction of two parameters of a custom control or in the construction of three parameters. In Java code, to get a custom attribute, we need to get it through the following code:
1 TypedArray a =2 buttonnum = A.getint (r.styleable.toolbar_buttonnum, 53 ITEMBG = A.getresourceid (R.styleable.toolbar_itembackground,-1); 4 ...... 5 a.recycle ();
get the value of a custom property in Java code
There's nothing to say here, just follow this code to take the value of the custom attribute. It is worth noting that after the completion of the value, we need to release the Typedarray, that is, the code in the a.recycle (); .
Custom properties of Android