Create custom attributes for Android

Source: Internet
Author: User
I. Res/ValuesFile defines Attrs. xmlFile. Code: <? XML version = "1.0" encoding = "UTF-8"?>
<Resources>
<Declare-styleable name = "customview">
<ATTR name = "Android: textcolor"/> // use the property name that comes with Android in custom Properties
<ATTR name = "customtextsize" format = "dimension"/> // custom attribute. The format attribute indicates the unit of the attribute.
</Declare-styleable>

</Resources>


II,InCustomview. JavaThe code is modified as follows. The following constructor is important. We can get the defined attributes.R. sytleable. customview_android_textcolor andR. sytleable. customview_customtextsize,The default value (Float textsize = A. getdimension (R. styleable.
Customview_customtextsize , 36 );),Prevent us fromXMLFile is not defined. Use the default value!

Get,CustomviewIs defined in<Declare-styleable name = "customview"> </declare-styleable>To obtain the attributes.Name_attributeConnect.TypedarrayUsually the last call. Recycle ()To maintain the consistency of future use of this attribute!

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 ){
Super (context );
Mpaint = new paint ();
// Todo auto-generated constructor stub
}
Public customview (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 ){
// 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,200,200), mpaint );

Mpaint. setcolor (color. Blue );
// Draw text
Canvas. drawtext (mstring, 10,110, mpaint );
}
}

 

3. DefineCustomviewAdd LayoutMain. xmlFile, and use custom properties, the custom properties must be added:

Xmlns: test = "http://schemas.android.com/apk/res/com.lee0000.AutoCustomAttr"BlueIs the prefix of custom attributes,RedIs our package name.Main. xmlThe Code is 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. leeapps.autocustomattr. customview
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Test: customtextsize = "20dp" // custom attributes
Android: textcolor = "# fff">
</COM. leeapps.autocustomattr. customview>
</Linearlayout>

 

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.