Android custom Control properties (Typedarray and attrs explanation)

Source: Internet
Author: User

Recently in tinkering with Android custom control properties, I learned about Typedarray and attrs. In this one read a great God blog Android in-depth understanding of custom properties in Android. I will be more in-depth learning power. I will follow this study, talk about the process, followed by a post and application.

1, attrs file preparation

<?xml version= "1.0" encoding= "Utf-8"?><resources>    <attr name= "TitleText" format= "string"/>    <attr name= "titletextcolor" format= "color"/> <attr name=    "titletextsize" format= "Dimension"/>    <declare-styleable name= "Authcodeview" >        <attr name= "TitleText"/> <attr name=        " Titletextcolor "/>        <attr name=" titletextsize "/>    </declare-styleable></resources>
See this above code has three attributes, first attr tag is to define the name and attributes. The following is a declare-styleable group, the group name Authcodeview, which is used in the following class.


2. How to reference and use in XML, compare system spatial Properties

Look at the two pictures first, understand the majority, also understand half.

A, a reference to the name of the custom attribute



b, take a closer look at the illustration and the comparison between A and B graphs. You know the property name change and how to reference it.



Afraid of the above image can not be seen, attached some XML code

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http//    Schemas.android.com/tools "xmlns:authcodeview=" Http://schemas.android.com/apk/res/com.example.authcodeview " Android:id= "@+id/linearlayout1" android:layout_width= "match_parent" android:layout_height= "Match_parent" Android: orientation= "vertical" > <linearlayout android:layout_width= "match_parent" android:layout_height= "W            Rap_content "> <com.example.authcodeview.view.authcodeview android:id=" @+id/authcodeview "            Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:padding= "10DP" authcodeview:titletext= "3712" authcodeview:titletextcolor= "#00ffff" Authcodeview:titletext Size= "40sp"/> <textview android:layout_width= "wrap_content" android:layout_height= "Wra   P_content "android:text=" Click the verification Code, change a "/> </LinearLayout> <linearlayout android:layout_width= "match_parent" android:layout_height= "Wrap_c Ontent "> <textview android:layout_width=" wrap_content "android:layout_height=" Wrap_con Tent "android:text=" Input verification Code "/> <edittext android:id=" @+id/edittext1 "Android: Layout_width= "Match_parent" android:layout_height= "Wrap_content" android:ems= "Ten" Android         : inputtype= "number" > <requestfocus/> </EditText> </LinearLayout> <button        Android:id= "@+id/button1" android:layout_width= "match_parent" android:layout_height= "Wrap_content" android:text= "Validate"/></linearlayout>


focus on the head layout of xmlns:android= "Http://schemas.android.com/apk/res/android" This is the role of reference system properties.

However xmlns:authcodeview= "Http://schemas.android.com/apk/res/com.example.authcodeview" is a reference to the custom attribute.

xmlns:+ name = "http://schemas.android.com/apk/res/+ App package name"

This is what happens when you use a custom property later.

authcodeview:titletext= "3712"
Authcodeview:titletextcolor= "#00ffff"
Authcodeview:titletextsize= "40SP"

By the way, attach the system arrs the custom path



3. How does class refer to a problem in a custom control?

Look at a piece of code first

/** * Get my Custom style properties * * @param context * @param attrs * @param defstyle */public Authcodeview (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, defstyle);/** * Get our Custom style properties defined */typedarray a = Context.gettheme (). obtainsty Ledattributes (Attrs, R.styleable.authcodeview, Defstyle, 0);//Get under the attr file, The declare-styleable attribute with the name Authcodeview has several int n = a.getindexcount (); for (int i = 0; i < n; i++) {int attr = A.getindex (i); SW Itch (attr) {//This property can not, because all are randomly generated case r.styleable.authcodeview_titletext:mtitletext = a.getstring (attr); break;case r.styleable.authcodeview_titletextcolor://default color set to Black Mtitletextcolor = A.getcolor (attr, color.black); break;case The r.styleable.authcodeview_titletextsize://default setting is 16sp,typevalue and you can convert the SP to Pxmtitletextsize = A.getdimensionpixelsize ( attr, (int) typedvalue.applydimension (typedvalue.complex_unit_sp, +, Getresources (). Getdisplaymetrics ())); break;}} A.recycle ();}


The role of this typedarray is the mapping of resources, the way it is written. R.styleable.authcodeview This is not very familiar.

And R.styleable.authcodeview_titletext, followed by the name plus the bottom line plus the attribute.

It's easy to do this by mapping the custom attribute to a class in XML settings.

There are spaces to look at to see the R file that generated the ID.


This is the end of this article, and this sequel, the custom property control, is also a custom view. Random Verification Code Demo learning.


Android custom Control properties (Typedarray and attrs explanation)

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.