A recent study on Android custom control properties has Typedarray and attrs. You can also learn from the article "Understanding the custom properties in Android" , followed by an application.
1, attrs document 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 the above code has three attributes, first attr tag is defined name and attribute. The following is a declare-styleable group whose name is Authcodeview and will be used in the following class.
2, in the XML how to reference and use, contrast system space attributes
first look at two pictures, you know most of it, but also understand more than 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 name of the property and how to reference it.
Afraid of the above picture can not see clearly, attached part of the 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= "Wrap_cont" Ent "> <com.example.authcodeview.view.authcodeview android:id=" @+id/authcodeview "android:layout_width=" WR Ap_content "android:layout_height=" wrap_content "android:padding=" 10DP "authcodeview:titletext=" 3712 "Authcode" View:titletextcolor= "#00ffff" authcodeview:titletextsize= "40sp"/> <textview android:layout_width= "Wrap_co Ntent "android:layout_height=" wrap_content "android:text=" click on the verification code, change a "/> </LinearLayout> <linearlay Out android:layout_width= "Match_parent" android:layout_height= "Wrap_content" > <textview android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:text= "Input verification Code"/> <edittext android:id= "@+id/edittext1" android:layout_width= "Match_parent" and roid: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= "Validation"/> </LinearLayout>
Focus on the xmlns:android= "Http://schemas.android.com/apk/res/android" in the head layout this is the role of referencing system attributes.
However, xmlns:authcodeview= "Http://schemas.android.com/apk/res/com.example.authcodeview" is a reference to a custom attribute.
xmlns:+ name = "http://schemas.android.com/apk/res/+ Applied Package name"
Custom attributes when used later that's it.
- authcodeview:titletext= "3712"
- Authcodeview:titletextcolor= "#00ffff"
- Authcodeview:titletextsize= "40SP"
Incidentally, attach the system Arrs custom path:
3, in the custom control class how to cite the problem
Read a piece of code first
/** * get my custom style attributes * * @param context * @param attrs * @param defstyle/Public Authcodeview
, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle); /** * Get the Custom style attribute we defined/TypedArray a = Context.gettheme (). Obtainstyledattributes (Attrs, R.styleable.authcodeview, D
Efstyle, 0);
Gets a number of int n = a.getindexcount () for the Declare-styleable property named Authcodeview under the attr file.
for (int i = 0; i < n; i++) {int attr = A.getindex (i);
Switch (attr) {//This property can not be, 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 R.styleable.authcodeview_titletextsize://The default setting is 16sp,typevalue can also convert sp to px mtitletextsize = A.getdimensionpixels
Ize (attr, (int) typedvalue.applydimension (typedvalue.complex_unit_sp, Getresources (). Getdisplaymetrics ()));
Break
}} a.recycle ();
}
The role of this typedarray is the mapping of resources, which is written in this way. R.styleable.authcodeview This is not very familiar.
There is also the R.styleable.authcodeview_titletext, followed by the name plus the next line plus attributes.
Doing so will map the custom attributes to the class in the XML settings, so it's easy to get them.
This is the end of the first here, and this sequel, custom attribute control, but also custom view, random verification code demo Learn more details please see "Android custom controls drill down to generate random verification code for Android".