Recently in the study of an open source project, see the definition of the resource file has the following tags:
In this project, the use of the above path traced back to such a class file, so confused, the definition of the layout file with the kind of hair relationship < comparison two > checked the next
The original is the use of custom attributes!
--------------------------------------------------------------------------------------------------------------- ---------
Look at a diagram first:
In fact, define a attrs.xml in the values directory, generate some components in the corresponding class file, and assign values to these properties in the layout file.
Cite an example of a cow:
Attrs.xml
"Data-sni Ppet-id= "EXT.E0895682196F048AD81F53E719476DBB" data-snippet-saved= "false" data-codota-status= "Done" ><?xml Version= "1.0" encoding= "Utf-8"?><resources> <attr name= "test1" format= "string"/> <declare-s Tyleable name= "MyView" > <attr name= "textcolor" format= "color"/> <attr name= "texts Ize "format=" Dimension "/> <attr name=" text "format=" string "/> </declare-styleable> </resources>
Explain:
attr: Defines the specific property, format represents the type of the value of this property, the type has the following types: 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 to be able to write values directly, you can also use a similar "@string/test" reference to resources, you can write format= "string|reference" 3.Color: Color 4.boolean: Boolean 5.dimension: Dimension value 6.float: Floating-point 7.integer: integer 8.fraction: Percent 9.enum: enumeration, if you provide a property can only let others choose, can not casually pass in, could write such <attr name= "L Anguage "> <enum name=" China "value=" 1 "/> <enum name=" 中文版 "value=" 2 "/&G T </attr> 10.flag: Bit or operand declare-styleable: Defines a Styleable object, each Styleable object is a set of attr properties Note: The Name property here does not have to be the same as the custom class name, just to distinguish the properties of the corresponding class. Note: After the property resource file above defines the property, as to which custom view component to use the property, the property does not belong to the property resource file tube, In other words, this property resource file is public and can be used by everyone, but for ease of administration, it is generally a custom view attribute written as a Declare-styleable collection. The properties defined by the property resource can return what effect, depending on the code implementation of the custom component
3. Defining 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 context) {super (context); Mpaint = new Paint (); } public MyView (Context Context,attributeset attrs) {super (context,attrs); Mpaint = new Paint (); /* Get declare-styleable set here */TypedArray Typearray = Context.obtainstyledattributes (Attrs,r.styleable.myview); /* Remove the corresponding attribute value from the collection, and the second parameter is the default value that the user uses when configuring the property */int textcolor = typearray.geTColor (R.STYLEABLE.MYVIEW_TEXTCOLOR,0XFFFFFFFF); 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 Resource */typearray.recycle (); } @Override protected void OnDraw (canvas canvas) {super.ondraw (canvas); Mpaint.setstyle (Style.fill); Canvas.drawrect (New Rect (ten, N, N), mpaint); Mpaint.setcolor (Color.Blue); Canvas.drawtext (Mstring, ten, mpaint); } }
4. layout File Reference Assignment
<?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 "a Ndroid:layout_width= "Fill_parent" android:layout_height= "fill_parent" ><textview android:layout_width= "fi Ll_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:textcol Or= "#ff0000" myandroid:textsize= "20px" myandroid:text= "http://wujiandong.iteye.com"/></linearlayout& Gt
Attention:
In Java code, the way to take property values, you must set a namespace for the custom component when XML uses it [xmlns:myandroid=] http://schemas.android.com/apk/res/ Cn.com.androidtest "], otherwise the component property cannot be set
Namespace notation: xmlns: space name = "Http://schemas.android.com/apk/res/Custom component Package Name"
There is also a place to note when writing a package name:
If your custom view package is similar to the two diagram below, then the package name can only be written as the topmost package [cn.com.androidtest], not [Cn.com.androidtest.ui]
Package tries to:
Detailed access to Daniel Blog: Click to view
android--Custom Properties