Use of custom attributes (ATTR. XML, typedarray) in Android

Source: Internet
Author: User
Tags getcolor

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. 1. Define an attrs. xml file under the Res/values file. The Code is as follows:
  2. <? XML version ="1.0"Encoding ="UTF-8"?>
  3. <Resources>
  4. <Declare-styleable name ="Myview">
  5. <ATTR name ="Textcolor"Format ="Color"/>
  6. <ATTR name ="Textsize"Format ="Dimension"/>
  7. </Declare-styleable>
  8. </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?
  1. PublicMyview (context, attributeset attrs)
  2. {
  3. Super(Context, attrs );
  4. Mpaint =NewPaint ();
  5. Typedarray A = context. obtainstyledattributes (attrs,
  6. R. styleable. myview );
  7. IntTextcolor = A. getcolor (R. styleable. myview_textcolor,
  8. 0 xffffffff);
  9. FloatTextsize = A. getdimension (R. styleable. myview_textsize,36);
  10. Mpaint. settextsize (textsize );
  11. Mpaint. setcolor (textcolor );
  12. A. Recycle ();
  13. }

Myview. Java
The Code is as follows:

View plaincopy to clipboardprint?
  1. PackageCom. Android. tutor;
  2. ImportAndroid. content. context;
  3. ImportAndroid. content. res. typedarray;
  4. ImportAndroid. Graphics. Canvas;
  5. ImportAndroid. Graphics. color;
  6. ImportAndroid. Graphics. paint;
  7. ImportAndroid. Graphics. rect;
  8. ImportAndroid. Graphics. Paint. style;
  9. ImportAndroid. util. attributeset;
  10. ImportAndroid. View. view;
  11. Public ClassMyviewExtendsView {
  12. PrivatePaint mpaint;
  13. PrivateContext mcontext;
  14. Private Static FinalString mstring ="Welcome to Mr Wei's blog";
  15. PublicMyview (context ){
  16. Super(Context );
  17. Mpaint =NewPaint ();
  18. }
  19. PublicMyview (context, attributeset attrs)
  20. {
  21. Super(Context, attrs );
  22. Mpaint =NewPaint ();
  23. Typedarray A = context. obtainstyledattributes (attrs,
  24. R. styleable. myview );
  25. IntTextcolor = A. getcolor (R. styleable. myview_textcolor,
  26. 0 xffffffff);
  27. FloatTextsize = A. getdimension (R. styleable. myview_textsize,36);
  28. Mpaint. settextsize (textsize );
  29. Mpaint. setcolor (textcolor );
  30. A. Recycle ();
  31. }
  32. @ Override
  33. Protected VoidOndraw (canvas ){
  34. // Todo auto-generated method stub
  35. Super. Ondraw (canvas );
  36. // Set Filling
  37. Mpaint. setstyle (style. Fill );
  38. // 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.
  39. Canvas. drawrect (NewRect (10,10,100,100), Mpaint );
  40. Mpaint. setcolor (color. Blue );
  41. // Draw text
  42. Canvas. drawtext (mstring,10,110, Mpaint );
  43. }
  44. }

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?
  1. <? XML
  2. Version ="1.0"Encoding ="UTF-8"?>
  3. <Linearlayout
  4. Xmlns: Android =Http://schemas.android.com/apk/res/android"
  5. Xmlns: test =Http://schemas.android.com/apk/res/com.android.tutor"
  6. Android: Orientation ="Vertical"
  7. Android: layout_width ="Fill_parent"
  8. Android: layout_height ="Fill_parent"
  9. >
  10. <Textview
  11. Android: layout_width ="Fill_parent"
  12. Android: layout_height ="Wrap_content"
  13. Android: text ="@ String/hello"
  14. />
  15. <Com. Android. Tutor. myview
  16. Android: layout_width ="Fill_parent"
  17. Android: layout_height ="Fill_parent"
  18. Test: textsize ="20px"
  19. Test: textcolor ="# Fff"
  20. />
  21. </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 ~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.