First, define a attrs.xml file under the res/values file. The code is as follows:
- <?xml version= "1.0" encoding= "Utf-8"?>
- <RESOURCES>&NBSP;&NBSP;
- <declare-styleable name= "MyView" >
- <attr name= "TextColor" format= "color" &NBSP;/>&NBSP;&NBSP;
- <attr name= "TextSize" Format= "Dimension" &NBSP;/>&NBSP;&NBSP;
- </declare-styleable>
- </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!
- Public MyView (Context 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,
- 0XFFFFFFFF);
- float textSize = a.getdimension (r.styleable.myview_textsize, 36);
- Mpaint.settextsize (textSize);
- Mpaint.setcolor (TextColor);
- A.recycle ();
- }
Myview.java MyView control all the code below
- 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 context) {
- super (context);
- Mpaint = new Paint ();
- }
- Public MyView (Context 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,
- 0XFFFFFFFF);
- float textSize = a.getdimension (r.styleable.myview_textsize, 36);
- Mpaint.settextsize (textSize);
- Mpaint.setcolor (TextColor);
- A.recycle ();
- }
- @Override
- protected void OnDraw (canvas canvas) {
- //TODO auto-generated method stub
- Super.ondraw (canvas);
- //Set fill
- Mpaint.setstyle (Style.fill);
- //Draw a rectangle, the first two is the upper-left corner of the rectangle coordinates, the following two are the lower right corner coordinates
- Canvas.drawrect (new Rect (Ten, ten, + ), mpaint);
- Mpaint.setcolor (Color.Blue);
- //Draw text
- Canvas.drawtext (mstring, Ten, mpaint);
- }
- }
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:
- <?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>
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