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

Source: Internet
Author: User
Tags getcolor

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. 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. 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
  1. Public myview (context, attributeset attrs)
  2. {
  3. Super (context, attrs );
  4. Mpaint = new paint ();
  5. Typedarray A = context. obtainstyledattributes (attrs,
  6. R. styleable. myview );
  7. Int textcolor = A. getcolor (R. styleable. myview_textcolor,
  8. 0 xffffffff );
  9. Float textsize = A. getdimension (R. styleable. myview_textsize, 36 );
  10. Mpaint. settextsize (textsize );
  11. Mpaint. setcolor (textcolor );
  12. A. Recycle ();
  13. }

 

Myview. JavaThe Code is as follows:

 

[Java]
View plaincopy
  1. Package com. Android. tutor;
  2. Import Android. content. context;
  3. Import Android. content. res. typedarray;
  4. Import Android. Graphics. Canvas;
  5. Import Android. Graphics. color;
  6. Import Android. Graphics. paint;
  7. Import Android. Graphics. rect;
  8. Import Android. Graphics. Paint. style;
  9. Import Android. util. attributeset;
  10. Import Android. View. view;
  11. Public class myview extends view {
  12. Private paint mpaint;
  13. Private context mcontext;
  14. Private Static final string mstring = "welcome to Mr Wei's blog ";
  15. Public myview (context ){
  16. Super (context );
  17. Mpaint = new paint ();
  18. }
  19. Public myview (context, attributeset attrs)
  20. {
  21. Super (context, attrs );
  22. Mpaint = new paint ();
  23. Typedarray A = context. obtainstyledattributes (attrs,
  24. R. styleable. myview );
  25. Int textcolor = A. getcolor (R. styleable. myview_textcolor,
  26. 0 xffffffff );
  27. Float textsize = A. getdimension (R. styleable. myview_textsize, 36 );
  28. Mpaint. settextsize (textsize );
  29. Mpaint. setcolor (textcolor );
  30. A. Recycle ();
  31. }
  32. @ Override
  33. Protected void ondraw (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 (New rect (10, 10,100,100), mpaint );
  40. Mpaint. setcolor (color. Blue );
  41. // Draw text
  42. Canvas. drawtext (mstring, 10,110, mpaint );
  43. }
  44. }

 

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
  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:

 

 

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.