To add a View that comes with Android on your user interface, you can specify this View in an XML Element and control its appearance and behavior through its corresponding element attributes.
Like a well-written custom View, you can add and set styles through XML. To add these actions for your custom View, you must:
1. <declare-styleable> The resource element defines the attributes of your View;
2. Specify the corresponding value for this attribute in your XML layout;
3. Retrieve the property value while running the program;
4. Use the retrieved property value in your View;
This section describes how to define custom attributes and specify their values. The next section describes how to obtain and use these values at runtime.
To customize attributes, add <declare-styleable> resources to your project. These resources are generally stored in the res/values/attrs. xml file,
Here is an example of the attrs. xml file:
<Resources>;
<Declare-styleable name = "PieChart">
<Attr name = "showText" format = "boolean"/>
<Attr name = "labelPosition" format = "enum">
<Enum name = "left" value = "0"/>
<Enum name = "right" value = "1"/>
</Attr>
</Declare-styleable>
</Resources> the Code declares two custom attributes: "showText" and "labelPosition", which belong to a style entity called PieChart. By convention, the style object name is the same as the declared custom view class name. Although it is not absolutely necessary to follow this Convention, many famous code writers provide declarations based on this naming convention.
Once you have defined custom attributes, you can use them in the layout XML file like built-in attributes. The only difference is that your custom attributes belong to different namespaces. They belong to the namespace to replace the default one. The following example shows how to use these defined attributes for PieChart:
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: custom = "http://schemas.android.com/apk/res/com.example.customviews">
<Com. example. customviews. charting. PieChart
Custom: showText = "true"
Custom: labelPosition = "left"/>
</LinearLayout> to avoid repeated use of long namespace Uris, an xmlns indicator is used in this example. This directive specifies the alias custom as the namespace http://schemas.android.com/apk/res/com.example.customviews you can select any alias for your namespace.
Note the name of the XML tag used to add custom views to the layout. This is the full expression of the custom view class. If your view contains an internal class, you must use the name of the external class to further limit it. For example, the PieChart class has an internal class called PieView. To use the Custom Attributes in this class, you must use the tag com. example. customviews. charting. PieChart $ PieView.
Application custom attributes:
After a view is created from the XML layout, all attributes in the XML tag are read from the resource package and passed to the view constructor as an AttributeSet. Although it is possible to directly read values from AttributeSet, it has some disadvantages:
If a resource reference with a value is not processed and cannot be replaced, the AttributeSet is passed to the obtainStyledAttributes () method. This method returns an array of TypedArray containing the unreferenced and styled values.
In order to make it easier to call the obtainStyledAttributes () method, the Android resource compiler has done a lot of work. For each <declare-styleable> resource in the res folder, the generated R. java defines an array of attribute IDs and a set of constants that define each attribute pointing to an array. You can use a predefined constant to read attributes from TypedArray. The following example shows how the PieChart class reads these attributes:
Public PieChart (Context ctx, AttributeSet attrs ){
Super (ctx, attrs );
TypedArray a = context. getTheme (). obtainStyledAttributes (
Attrs,
R. styleable. PieChart,
0, 0 );
Try {
MShowText = a. getBoolean (R. styleable. PieChart_showText, false );
MTextPos = a. getInteger (R. styleable. PieChart_labelPosition, 0 );
} Finally {
A. recycle ();
}
} Note: The TypedArray object is a shared resource and must be recycled after use.