Android Create custom properties

Source: Internet
Author: User

First, in res/valuesThe file defines a Attrs.xmlFile. The code is as follows:<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
<declare-styleable name= "CustomView" >
<attr name= "Android:textcolor"/>//use Android-brought property names in custom properties
<attr name= "Customtextsize" format= "Dimension"/>//custom property, the Format property represents the unit of the property
</declare-styleable>

</resources>

Second, we modify the Customview.java code as follows, where the following construction method is the focus, we get the defined properties r.sytleable.customview_android_ TextColor and r.sytleable.customview_customtextsize, gets the default values that are typically set later in the method (float textSize = a.getdimension ( R.styleable. customview_customtextsize , +); )to prevent us from being defined in the XML file. Thus using the default value!

Get,CustomView is the name defined in <declare-styleable name= "CustomView" ></declare-styleable> , get the Inside property with the name _ property to connect together. TypedArray usually last called the . Recycle () method, in order to keep the attribute consistent after use!

Import Android.content.Context;

Import Android.content.res.TypedArray;
Import Android.graphics.Canvas;
Import Android.graphics.Color;
Import Android.graphics.Paint;
Import Android.graphics.Paint.Style;
Import Android.graphics.Rect;
Import Android.util.AttributeSet;
Import Android.view.View;

public class CustomView extends view{
Private Paint Mpaint;
private static final String mstring = "Welcome to Mr Wei ' s blog";
Public CustomView (Context context) {
Super (context);
Mpaint = new Paint ();
TODO auto-generated Constructor stub
}
Public CustomView (context context, AttributeSet Attrs) {
Super (context, attrs);
Mpaint = new Paint ();
TypedArray a = Context.obtainstyledattributes (Attrs, R.styleable.customview);

int textcolor = A.getcolor (R.styleable.customview_android_textcolor, 0xff0000);
float textSize = a.getdimension (r.styleable.customview_customtextsize, 36);

Mpaint.setcolor (TextColor);
Mpaint.settextsize (textSize);

A.recycle ();
}
@Override
protected void OnDraw (canvas canvas) {
TODO auto-generated Method Stub
Super.ondraw (canvas);
Set the Fill
Mpaint.setstyle (Style.fill);

Draw a rectangle, the first two are the coordinates of the upper left corner of the rectangle, the next two are the lower right corner coordinates
Canvas.drawrect (New Rect (Ten, Ten, Max), mpaint);

Mpaint.setcolor (Color.Blue);
Draw text
Canvas.drawtext (Mstring, ten, mpaint);
}
}

Third, add our custom CustomView to the layout main.xml file, flat and use custom attributes, custom attributes must be added:

xmlns:test = "http://schemas.android.com/apk/res/com.lee0000.AutoCustomAttr " Blue is the prefix of the custom attribute, red 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.lee0000.AutoCustomAttr"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:orientation= "Vertical" >

<textview
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "@string/hello"/>

<com.lee0000.autocustomattr.customview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Test:customtextsize= "20DP"//Custom properties
Android:textcolor= "#fff" >
</com.lee0000.AutoCustomAttr.customView>
</LinearLayout>


Previous Demo Download: Customattrexample.zip

Android Create custom properties

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.