Android Phone Defender--custom properties

Source: Internet
Author: User

In the previous article, the function and layout of the first column in the Settings center have been implemented

This article address: http://www.cnblogs.com/wuyudong/p/5936016.html, reprint please indicate the source.

Custom attribute declarations

The next step is to implement the layout and functionality of the other columns, because the functions and layouts are similar, except for the property names. So this article implements custom properties on the basis of custom controls

First refer to the standard Control source code, here Select TextView

The source path is: D:\adt-bundle-windows-x86_64_20140101\sdk\platforms\android-18\data\res\values

Open the Attrs.xml file under this folder to find the following code:

    <declare-styleablename= "TextView">        <!--determines the minimum type that getText () would return.             The default is "normal". Note that EditText and Logtextbox always return Editable, even if you specify something less powerful here.  -        <attrname= "Buffertype">            <!--Can return any charsequence, possibly a spanned one if the source text is spanned. -            <enumname= "normal"value= "0" />            <!--Can only return spannable. -            <enumname= "Spannable"value= "1" />            <!--Can only return spannable and Editable. -            <enumname= "Editable"value= "2" />        </attr>        <!--Text to display. -        <attrname= "text"format= "string"localization= "suggested" />        <!--Hint Text to display when the text is empty. -        <attrname= "hint"format= "string" />

So we can also imitate the key node, write out the custom properties, the project Res\values folder under the new Attrs.xml file, add code as follows:

<?XML version= "1.0" encoding= "Utf-8"?><Resources>    <declare-styleablename= "Com.wuyudong.mobilesafe.view.SettingItemView">        <attrname= "Destitle"format= "string"/>        <attrname= "Desoff"format= "string"/>        <attrname= "Deson"format= "string"/>    </declare-styleable></Resources>
To get custom property values in the constructor method

Next define the namespace, also refer to the Android standard to write

<? XML version= "1.0" encoding= "Utf-8" ?> <  xmlns:android= "http://schemas.android.com/apk/res/android"... 

Mobilesafe Replace the original Android

Com.wuyudong.mobilesafe must be written like this, replacing Android, representing the currently applied custom attribute
Xmlns:mobilesafe= "Http://schemas.android.com/apk/res/com.wuyudong.mobilesafe"

The modified code is as follows:

    <Com.wuyudong.mobilesafe.view.SettingItemView xmlns:mobilesafe= "Http://schemas.android.com/apk/res/com.wuyudong.mobilesafe" Android:id= "@+id/siv_update"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content" mobilesafe:destitle= "Automatic Update settings"  mobilesafe:desoff= "Automatic Update is off"  Mobilesafe:deson= "Automatic Update is turned on" >     </Com.wuyudong.mobilesafe.view.SettingItemView>

Custom attribute values have been taken care of, now is the Settingitemview class how to get these values?

Call the Initattrs function in the constructor of the Settingitemview class, and then implement the return of the property through the Initattrs function. Get familiar with the API with a few small practices first

   /*** Returns the property value of a custom attribute in the property collection *@paramAttrs The set of properties maintained in the construction method*/    Private voidinitattrs (AttributeSet attrs) {//gets the total number of attributesLOG.I (Tag, "Attrs.getattributecount ():" +Attrs.getattributecount ()); //Get property names and property values         for(inti = 0; I < Attrs.getattributecount (); i++) {LOG.I (tag,"Name =" +Attrs.getattributename (i)); LOG.I (Tag,"VALUE =" +Attrs.getattributevalue (i)); LOG.I (Tag,"================== Split Line ======================"); }    }

After you run the project, print the following log information in Logcat:

Explain some of the above code:

Value = @2131427413 corresponds to a hexadecimal value of: 7f0b0055, which is actually the corresponding R file

 Public Static Final int siv_update=0x7f0b0055;

Everything else is simple.

Then we use the other API to get the value of the property

Private Static FinalString NAMESPACE = "Http://schemas.android.com/apk/res/com.wuyudong.mobilesafe";........... /*** Returns the property value of a custom attribute in the property collection *@paramAttrs The set of properties maintained in the construction method*/    Private voidinitattrs (AttributeSet attrs) {/*//Gets the total number of attributes log.i (tag, "Attrs.getattributecount ():" +attrs.getattributecount ()); Gets the property name and attribute value for (int i = 0; i < Attrs.getattributecount (); i++) {LOG.I (tag, "name =" + Attrs.geta            Ttributename (i));            LOG.I (Tag, "value =" + Attrs.getattributevalue (i));        LOG.I (Tag, "================== Split Line ======================"); }*/String Destitle= Attrs.getattributevalue (NAMESPACE, "Destitle"); String Desoff= Attrs.getattributevalue (NAMESPACE, "Desoff"); String Deson= Attrs.getattributevalue (NAMESPACE, "Deson");        LOG.I (tag, destitle);        LOG.I (tag, desoff); LOG.I (tag, deson);    }

After you run the project, print the following log information in Logcat:

Indicates that the property value set has been successfully obtained

This allows you to reuse the code to implement the layout of the phone attribution in the second column.

    <Com.wuyudong.mobilesafe.view.SettingItemViewXmlns:mobilesafe= "Http://schemas.android.com/apk/res/com.wuyudong.mobilesafe"Android:id= "@+id/siv_update"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Mobilesafe:destitle= "Automatic Update settings"Mobilesafe:desoff= "Automatic Update is OFF"Mobilesafe:deson= "Automatic Update is turned on" >    </Com.wuyudong.mobilesafe.view.SettingItemView>    <Com.wuyudong.mobilesafe.view.SettingItemViewXmlns:mobilesafe= "Http://schemas.android.com/apk/res/com.wuyudong.mobilesafe"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Mobilesafe:destitle= "Display settings for phone attribution"Mobilesafe:desoff= "Display of attribution closed"Mobilesafe:deson= "The display of attribution is turned on" >    </Com.wuyudong.mobilesafe.view.SettingItemView>

Android Phone Defender--custom properties

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.