Ways to customize controls and properties (Attr.xml,declare-styleable,typedarray) in Android and use

Source: Internet
Author: User

First, define a attrs.xml file under the res/values file. The code is as follows:

    1. <?xml version= "1.0"  encoding= "Utf-8"?>  
    2. <RESOURCES>&NBSP;&NBSP;
    3.     <declare-styleable name= "MyView" >  
    4.         <attr name= "TextColor"  format= "color" &NBSP;/>&NBSP;&NBSP;
    5.         <attr name= "TextSize"   Format= "Dimension" &NBSP;/>&NBSP;&NBSP;
    6.      </declare-styleable>  
    7. </resources>  

Second , we write the following in the Myview.java code, where the following construction method is the focus, we get the defined property R.sytleable.myview_textcolor, The default value is usually set later in the Get method (float textSize = a.getdimension (r.styleable.myview_textsize); ) to prevent us from being defined in the XML file. Use default values!

MyView is defined in <declare-styleable name= "MyView" ></declare-styleable> name, get inside the attribute with The name _ property can be connected together. TypedArray usually last called the . Recycle () method, in order to keep the attribute consistent after use!

  1. Public MyView (Context 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. 0XFFFFFFFF);
  9. float textSize = a.getdimension (r.styleable.myview_textsize, 36);
  10. Mpaint.settextsize (textSize);
  11. Mpaint.setcolor (TextColor);
  12. A.recycle ();
  13. }

Myview.java MyView control all the code below

  1. Public class MyView extends View {
  2. private Paint Mpaint;
  3. private Context Mcontext;
  4. private static final String mstring = "Welcome to Mr Wei ' s blog";
  5. Public MyView (context context) {
  6. super (context);
  7. Mpaint = new Paint ();
  8. }
  9. Public MyView (Context context,attributeset attrs)
  10. {
  11. super (CONTEXT,ATTRS);
  12. Mpaint = new Paint ();
  13. TypedArray a = Context.obtainstyledattributes (Attrs,
  14. R.styleable.myview);
  15. int textcolor = A.getcolor (R.styleable.myview_textcolor,
  16. 0XFFFFFFFF);
  17. float textSize = a.getdimension (r.styleable.myview_textsize, 36);
  18. Mpaint.settextsize (textSize);
  19. Mpaint.setcolor (TextColor);
  20. A.recycle ();
  21. }
  22. @Override
  23. protected void OnDraw (canvas canvas) {
  24. //TODO auto-generated method stub
  25. Super.ondraw (canvas);
  26. //Set fill
  27. Mpaint.setstyle (Style.fill);
  28. //Draw a rectangle, the first two is the upper-left corner of the rectangle coordinates, the following two are the lower right corner coordinates
  29. Canvas.drawrect (new Rect (Ten, ten, + ), mpaint);
  30. Mpaint.setcolor (Color.Blue);
  31. //Draw text
  32. Canvas.drawtext (mstring, Ten, mpaint);
  33. }
  34. }

Third, add our custom MyView to the layout main.xml file, and with custom attributes, the custom attribute must be added:

" xmlns:test =" Http://schemas.android.com/apk/res/com.android.tutor ", test is the prefix of the custom attribute, Com.android.tutor It's our package name.

Main.xml All codes are as follows:

  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>

Format, which is the format of this property, is listed below in the approximate format:

1. Reference: Refer to a resource ID, etc.

(1) Attribute definition:

(2) attribute use:

<imageview< p= "" >

Android:layout_width = "42dip"

Android:layout_height = "42dip"

Android:background = "@drawable/Picture ID"

/>

2. Color: Colour value

3. Boolean: Boolean value

4. Dimension: Dimension value. Note that if this is DP, then the pixel conversion will be done.

5. Float: floating-point value.

6. Integer: Integer value.

7. String: Strings

8. Fraction: percentage.

9. Enum: enumeration value

Flag: It is defined by itself, similar to the android:gravity= "top", which corresponds to its own attribute value.

Reference|color: A resource file for color . 12.reference|boolean: boolean-Valued resource file

Note://Because reference is obtained from the resource file: So you must personattr:name= the"@string/app_name" format when you write this attribute in the XML file, or you will get an error

Ways to customize controls and properties (Attr.xml,declare-styleable,typedarray) in Android and use

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.