Android custom controls (3) Custom Attributes

Source: Internet
Author: User
Tags drawtext getcolor

Book connection

Create attributes in XML, and use typedarray in Java code to obtain these attributes. After obtaining the attributes, use them to do something. For example, get the color in XML and assign it to paint.

1. Create attrs. xml under Res/values/

<? XML version = "1.0" encoding = "UTF-8"?> <Resources> <declare-styleable name = "customview2"> <ATTR name = "textcolor" format = "color"/> <ATTR name = "textsize" format = "dimension "/> </declare-styleable> </resources> <! -- Name = "customview1" Control name --> <! -- Name = "textcolor" corresponds to test: textcolor --> <! -- Format = "color" corresponds to a. getcolor (R. styleable. customview2_textcolor, 0 xffffffff) in the constructor; -->

Detailed description of format can refer to the http://blog.csdn.net/ethan_xue/article/details/7315064
2. Mainly look at Constructors

Public class customview2 extends view {private paint mpaint2; private string mtext = "drawtext"; Public customview2 (context, attributeset attrs) {super (context, attrs ); mpaint2 = new paint (); // typedarray is the array that stores Resources, 1. get this array through context. attrs is passed in by the constructor, corresponding to attrs. xmltypedarray A = context. obtainstyledattributes (attrs, R. styleable. customview2); // get the attributes defined in XML. The format is name_attribute name followed by the default value int textcolor =. getcolor (R. styleable. customview2_textcolor, 0 xffffffff); float textsize =. getdimension (R. styleable. customview2_textsize, 35); mpaint2.setcolor (textcolor); mpaint2.settextsize (textsize); // in order to maintain the consistency of future use of this attribute, a signal indicating the end of the bound resource is returned to resource. recycle () ;}@ overrideprotected void ondraw (canvas) {super. ondraw (canvas); mpaint2.setstyle (style. fill); canvas. drawtext (mtext, 10, 60, mpaint2 );}}

3. Layout

<? XML version = "1.0" encoding = "UTF-8"?> <! -- Xmlns: test = "http://schemas.android.com/apk/res/ethan.customview1" package name --> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" xmlns: test = "http://schemas.android.com/apk/res/ethan.customview1" Android: Orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <Ethan. customview1.customview2 Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" test: textcolor = "# f00" test: textsize = "20sp"/> </linearlayout>

4.

Http://download.csdn.net/detail/ethan_xue/4108832

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.