Recently I was studying an open-source project and found that the resource files defined by others have the following labels:
In this project, the above path tracing will be traced back to such a class file, so I am confused that the definition of the layout file has a hairy relationship with the class <comparison 2> I checked
It turns out to be the use of custom properties!
Bytes ------------------------------------------------------------------------------------------------------------------------
First look at a diagram:
In fact, define an attrs. xml file under the values directory, generate some components in the corresponding class file, and assign values to these attributes in the layout file.
Here is an example of a cool man:
Attrs. xml
<?xml version="1.0" encoding="utf-8"?><resources> <attr name="test1" format="string" /> <declare-styleable name="MyView"> <attr name="textColor" format="color" /> <attr name="textSize" format="dimension" /> <attr name="text" format="string" /> </declare-styleable> </resources>
Explanation:
ATTR sub-element: defines a specific attribute. Format indicates the type of the value of this attribute. The types include: 1. reference: refer to the resource ID in the specified theme. This type means that the value you pass can be a reference resource 2. string: String. If you want others to directly write a value or use a method similar to "@ string/test" to reference a resource, you can write it as format = "string | reference" 3. color: Color 4. boolean: Boolean value 5. dimension: size value 6. float: Float 7. integer: integer 8. fraction: Percentage 9. enum: enumeration. If you provide attributes that can only be selected by others, <ATTR name = "language"> <Enum name = "China" value = "1"/> <Enum name = "English" value = "2"/> </ATTR> 10. flag: bit or operation declare-styleable sub-element: defines a styleable object. Each styleable object is a set of ATTR attributes. Note: the name attribute here is not necessarily the same as the custom class name, just to distinguish the attributes of the corresponding class. Note: After the attribute resource file above defines this attribute, as to which custom view component is used to use this attribute, the resource file of this attribute is not responsible for its role. That is to say, this property resource file is public, you can use it. However, for ease of management, attributes in a custom view are generally written as a declare-styleable set. what can a property defined by a property resource return? It depends on the Code Implementation of the custom component.
3. Define component classes
Package cn.com. androidtest. ui; import cn.com. androidtest. r; import android. content. context; import android. content. res. typedarray; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. graphics. paint. style; import android. graphics. rect; import android. util. attributeset; import android. view. view; public class myview extends view {private paint mpaint; private context mcontext; Private Static string mstring; private string test; Public myview (context) {super (context ); mpaint = new paint ();} public myview (context, attributeset attrs) {super (context, attrs); mpaint = new paint (); /* obtain the declare-styleable Set */typedarray typearray = context. obtainstyledattributes (attrs, R. styleable. myview);/* the corresponding property value is retrieved from the set. The second parameter is the default value */INT textcolor = typearray if the property is not configured. getcolor (R. styleable. myview_textcolor, 0 xffffffff); float textsize = typearray. getdimension (R. styleable. myview_textsize, 36); mstring = typearray. getstring (R. styleable. myview_text);/* set your own class member variable */mpaint. settextsize (textsize); mpaint. setcolor (textcolor);/* close Resources */typearray. recycle () ;}@ override protected void ondraw (canvas) {super. ondraw (canvas); mpaint. setstyle (style. fill); canvas. drawrect (New rect (10, 10, 90, 90), mpaint); mpaint. setcolor (color. blue); canvas. drawtext (mstring, 10,110, mpaint );}}
4. Assignment of layout file references
<?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/cn.com.androidtest" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" ><TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello"/> <cn.com.androidtest.ui.MyView android:layout_width="fill_parent" android:layout_height="wrap_content" myandroid:textColor="#ff0000" myandroid:textSize="20px" myandroid:text="http://wujiandong.iteye.com"/></LinearLayout>
Note:
In Java code, you must set a namespace for the custom component when using the component in XML [xmlns: myandroid = "http://schemas.android.com/apk/res/cn.com.androidtest"], otherwise, component properties cannot be set.
Namespace Syntax: xmlns: space name = "http://schemas.android.com/apk/res/self-defined kit package name"
Note the following when writing the package name:
If the package of your custom view is similar to the following two figures, the package name can only be written as the top-level package [cn.com. androidtest], not [cn.com. androidtest. UI]
Package attempt:
Go to Daniel's blog in detail: Click to View Details