Android advanced tutorial (4) ---- Android custom attributes (attr. xml, TypedArray!

Source: Internet
Author: User

Today, our tutorial is extended according to the previous section. If you haven't read it, please click html "target = _ blank> Android advanced tutorial (3) to view Lesson 3, this is easy for you to understand!

Defining control attributes in xml files is a habit of android: attrs = "". Can we define our own attributes, such as test: attrs =? The answer is yes.

Well, I will not turn to the topic. I will go directly to the topic. Perform the following steps:

1. Define an attrs. xml file under the res/values file. The Code is as 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>

2. In MyView. the java code is modified as follows. The following constructor is the focus. We get the defined attribute R. sytleable. myView_textColor. The default value (float textSize =. getDimension (R. styleable. myView_textSize, 36);) to prevent definition in the xml file. to use the default value!

Get, MyView is defined in the name defined in <declare-styleable name = "MyView"> </declare-styleable>. You can get the attributes in it and connect them with the name_attribute. typedArray is usually called at the end. the recycle () method. To maintain future consistency with this attribute!

View plaincopy to clipboardprint?
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,
0 XFFFFFFFF );
Float textSize = a. getDimension (R. styleable. MyView_textSize, 36 );

MPaint. setTextSize (textSize );
MPaint. setColor (textColor );

A. recycle ();
}

All the code for MyView. java is as follows:

View plaincopy to clipboardprint?
Package com. android. tutor;
Import android. content. Context;
Import android. content. res. TypedArray;
Import android. graphics. Canvas;
Import android. graphics. Color;
Import android. graphics. Paint;
Import android. graphics. Rect;
Import android. graphics. Paint. Style;
Import android. util. AttributeSet;
Import android. view. View;
Public class MyView extends View {
Private Paint mPaint;
Private Context mContext;
Private static final String mString = "Welcome to Mr Weis 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,
0 XFFFFFFFF );
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 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 (new Rect (10, 10,100,100), mPaint );

MPaint. setColor (Color. BLUE );
// Draw text
Canvas. drawText (mString, 10,110, mPaint );
}
}

3. Add the custom MyView to the layout main. xml file. Use the custom attributes equally. The Custom Attributes must be added:

Xmlns: test = "http://schemas.android.com/apk/res/com.android.tutor" Blue is the prefix of custom properties, red is our package name.

The code for main. xml 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:

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.