From: http://blog.csdn.net/android_tutor/article/details/5508615
InXMLWe are used to defining the properties of controls in the file.Android: attrs = ""Can we define our own attributes, for example:Test: attrs = ""What about it? The answer is yes.
Well, I will not turn to the topic. I will go directly to the topic. Perform the following steps:
I,InRes/ValuesFile definesAttrs. xmlFile. The Code is as follows:
[Java]
View plaincopy
- 1. Define an attrs. xml file under the Res/values file. The Code is as follows:
- <? XML version = "1.0" encoding = "UTF-8"?>
- <Resources>
- <Declare-styleable name = "myview">
- <ATTR name = "textcolor" format = "color"/>
- <ATTR name = "textsize" format = "dimension"/>
- </Declare-styleable>
- </Resources>
II,InMyview. JavaThe code is modified as follows. The following constructor is the focus, and we get the defined attributes.R. sytleable. myview_textcolor,The default value (Float textsize = A. getdimension (R. styleable. myview_textsize,
36 );),Prevent us fromXMLFile is not defined. Use the default value!
Get,MyviewIs defined in<Declare-styleable name = "myview"> </declare-styleable>To obtain the attributes.Name_attributeConnect.TypedarrayUsually the last call. Recycle ()To maintain the consistency of future use of this attribute!
[Java]
View plaincopy
- Public myview (context, attributeset attrs)
- {
- Super (context, attrs );
- Mpaint = new paint ();
- Typedarray A = context. obtainstyledattributes (attrs,
- R. styleable. myview );
- Int textcolor = A. getcolor (R. styleable. myview_textcolor,
- 0 xffffffff );
- Float textsize = A. getdimension (R. styleable. myview_textsize, 36 );
- Mpaint. settextsize (textsize );
- Mpaint. setcolor (textcolor );
- A. Recycle ();
- }
Myview. JavaThe Code is as follows:
[Java]
View plaincopy
- Package com. Android. tutor;
- Import Android. content. context;
- Import Android. content. res. typedarray;
- Import Android. Graphics. Canvas;
- Import Android. Graphics. color;
- Import Android. Graphics. paint;
- Import Android. Graphics. rect;
- Import Android. Graphics. Paint. style;
- Import Android. util. attributeset;
- Import Android. View. view;
- Public class myview extends view {
- Private paint mpaint;
- Private context mcontext;
- Private Static final string mstring = "welcome to Mr Wei's blog ";
- Public myview (context ){
- Super (context );
- Mpaint = new paint ();
- }
- Public myview (context, attributeset attrs)
- {
- Super (context, attrs );
- Mpaint = new paint ();
- Typedarray A = context. obtainstyledattributes (attrs,
- R. styleable. myview );
- Int textcolor = A. getcolor (R. styleable. myview_textcolor,
- 0 xffffffff );
- Float textsize = A. getdimension (R. styleable. myview_textsize, 36 );
- Mpaint. settextsize (textsize );
- Mpaint. setcolor (textcolor );
- A. Recycle ();
- }
- @ Override
- Protected void ondraw (canvas ){
- // Todo auto-generated method stub
- Super. ondraw (canvas );
- // Set Filling
- Mpaint. setstyle (style. Fill );
- // Draw a rectangle. The first two are the coordinates in the upper left corner of the rectangle, and the last two are the coordinates in the lower right corner.
- Canvas. drawrect (New rect (10, 10,100,100), mpaint );
- Mpaint. setcolor (color. Blue );
- // Draw text
- Canvas. drawtext (mstring, 10,110, mpaint );
- }
- }
3. DefineMyviewAdd LayoutMain. xmlFile, and use custom properties, the custom properties must be added:
Xmlns: test = "http://schemas.android.com/apk/res/com.android.tutor"BlueIs the prefix of custom attributes,RedIs our package name.
Main. xmlThe Code is as follows:
[Java]
View plaincopy
- <? XML
- Version = "1.0" encoding = "UTF-8"?>
- <Linearlayout
- Xmlns: Android = "http://schemas.android.com/apk/res/android"
- Xmlns: test = "http://schemas.android.com/apk/res/com.android.tutor"
- 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"
- />
- <Com. Android. Tutor. myview
- Android: layout_width = "fill_parent"
- Android: layout_height = "fill_parent"
- Test: textsize = "20px"
- Test: textcolor = "# fff"
- />
- </Linearlayout>
4. The running effect is as follows: