Android advanced tutorial (4) ---- Android custom attributes (ATTR. XML, typedarray!
Favorites
Our tutorial is based on the extension in the previous section. If you haven't read it, clickAndroid advanced tutorial (3)
Check the third lesson to make it easy for you to understand!
InXML
We 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/Values
File definesAttrs. xml
File.CodeAs follows:
View plaincopy to clipboardprint?
-
- 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. Java
The 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 fromXML
File is not defined. Use the default value!
Get,Myview
Is defined in<Declare-styleable name ="Myview
"> </Declare-styleable>
In
Name, used to obtain the attributesName_attribute
Connect.Typedarray
Usually the last call. Recycle ()
To maintain the consistency of future use of this attribute!
View plaincopy to clipboardprint?
- PublicMyview (context, attributeset attrs)
-
- {
-
- Super(Context, attrs );
-
- Mpaint =NewPaint ();
-
-
- Typedarray A = context. obtainstyledattributes (attrs,
- R. styleable. myview );
-
-
- IntTextcolor = A. getcolor (R. styleable. myview_textcolor,
-
- 0 xffffffff);
-
- FloatTextsize = A. getdimension (R. styleable. myview_textsize,36);
-
- Mpaint. settextsize (textsize );
-
- Mpaint. setcolor (textcolor );
-
-
- A. Recycle ();
-
- }
Myview. Java
The Code is as follows:
View plaincopy to clipboardprint?
-
- PackageCom. Android. tutor;
- ImportAndroid. content. context;
-
- ImportAndroid. content. res. typedarray;
-
- ImportAndroid. Graphics. Canvas;
-
- ImportAndroid. Graphics. color;
-
- ImportAndroid. Graphics. paint;
-
- ImportAndroid. Graphics. rect;
-
- ImportAndroid. Graphics. Paint. style;
- ImportAndroid. util. attributeset;
-
- ImportAndroid. View. view;
-
- Public ClassMyviewExtendsView {
-
- PrivatePaint mpaint;
-
- PrivateContext mcontext;
- Private Static FinalString mstring ="Welcome to Mr Wei's blog";
-
-
- PublicMyview (context ){
-
- Super(Context );
-
- Mpaint =NewPaint ();
- }
-
- PublicMyview (context, attributeset attrs)
-
- {
-
- Super(Context, attrs );
-
- Mpaint =NewPaint ();
-
-
- Typedarray A = context. obtainstyledattributes (attrs,
- R. styleable. myview );
-
-
- IntTextcolor = A. getcolor (R. styleable. myview_textcolor,
-
- 0 xffffffff);
-
- FloatTextsize = A. getdimension (R. styleable. myview_textsize,36);
-
- Mpaint. settextsize (textsize );
-
- Mpaint. setcolor (textcolor );
-
-
- A. Recycle ();
-
- }
-
- @ Override
- Protected VoidOndraw (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 (NewRect (10,10,100,100), Mpaint );
-
-
- Mpaint. setcolor (color. Blue );
-
- // Draw text
- Canvas. drawtext (mstring,10,110, Mpaint );
-
- }
-
- }
3. DefineMyview
Add LayoutMain. xml
File, and use custom properties, the custom properties must be added:
Xmlns:Test
= "Http://schemas.android.com/apk/res/Com. Android. Tutor
"Blue
Is the prefix of custom attributes,Red
Is our package name.
Main. xml
The Code is as follows:
View plaincopy to clipboardprint?
- <? 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:
Today is the end. If you have any questions, please leave a message. I will reply to you in time! Thank you ~