Recently, I've been tinkering with Android to define control properties and learn Typedarray and attrs. In this one read a great God blog Android in-depth understanding of their own defined properties in Android. I will be more in-depth learning power. I will go along this study, talk about the process, perhaps an article also has the 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. Control system Spatial Properties
Look at two pictures first. I know half of it, and I understand half of it.
A, a reference to the name of the property you define
b, a detailed view of the figure and A and B chart comparison. You know the property name is changed. and how to quote.
Afraid of the above picture can not see clearly. Attach 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 its own defined attribute.
xmlns:+ name = "http://schemas.android.com/apk/res/+ App package name"
When you use it, you define the property.
authcodeview:titletext= "3712"
Authcodeview:titletextcolor= "#00ffff"
Authcodeview:titletextsize= "40SP"
By the way, attach the system arrs the path you define.
Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center ">
3. How does the class reference the problem in its own definition control?
Look at a piece of code first
/** * get my own defined style properties * * @param context * @param attrs * @param defstyle */public Authcodeview (context context, Attributese T attrs, int defstyle) {Super (context, attrs, defstyle);/** * get our own defined style attributes */typedarray a = Context.gettheme (). Obtains Tyledattributes (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 R.STYLEABLE.AUTHCODEVIEW_TITLETEXTSIZE://is set to 16SP by default. Typevalue can also convert sp to pxmtitletextsize = A.getdimensionpixelsize (attr, (int) typedvalue.applydimension ( TYPEDVALUE.COMPLEX_UNIT_SP, Getresources () getdisplaymetrics ()); A.recycle ();}
R.styleable.authcodeview This is not very familiar.
It is very easy to do this by mapping your own defined properties to the XML setting values to class. &NBSP
There's a space to look at the R file that generated the ID.
This one ends here first, And the sequel to this article. Define the property control yourself. Also define the view yourself. Random Verification Code Demo learning.
Android self-defined control properties (Typedarray and Attrs interpretation)