Android mobile guard-custom attributes, android guard

Source: Internet
Author: User

Android mobile guard-custom attributes, android guard

In the previous article, the function and layout of the first column of "setting Center" have been implemented.

For more information, see http://www.cnblogs.com/wuyudong/p/5936016.html.

Custom attribute Declaration

Next, we will implement the layout and functions of other columns. The functions are similar to the layout, but the attribute names are different. Therefore, this article implements custom attributes based on custom controls.

First, refer to the source code of the standard control. Select TextView

Source code path: D: \ adt-bundle-windows-x86_64_20140101 \ sdk \ platforms \ android-18 \ data \ res \ values

Open the attrs. xml file in this folder and find the following code:

    <declare-styleable name="TextView">        <!-- Determines the minimum type that getText() will return.             The default is "normal".             Note that EditText and LogTextBox always return Editable,             even if you specify something less powerful here. -->        <attr name="bufferType">            <!-- Can return any CharSequence, possibly a             Spanned one if the source text was Spanned. -->            <enum name="normal" value="0" />            <!-- Can only return Spannable. -->            <enum name="spannable" value="1" />            <!-- Can only return Spannable and Editable. -->            <enum name="editable" value="2" />        </attr>        <!-- Text to display. -->        <attr name="text" format="string" localization="suggested" />        <!-- Hint text to display when the text is empty. -->        <attr name="hint" format="string" />

So we can also imitate key nodes and write custom attributes. In the res \ values folder of the project, create the attrs. xml file and add the following code:

<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="com.wuyudong.mobilesafe.view.SettingItemView">        <attr name="destitle" format="string"/>        <attr name="desoff" format="string"/>        <attr name="deson" format="string"/>    </declare-styleable></resources>
Get custom attribute values in Constructor

Next, define the namespace and write it based on the android standard.

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"……

Mobilesafe replaces the original android

Com. wuyudong. mobilesafe must be written in this way. If android is replaced, it indicates the custom attributes of the current application.
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 = "auto update settings" mobilesafe: desoff = "auto update disabled" mobilesafe: deson = "auto update enabled"> </com. wuyudong. mobilesafe. view. settingItemView>

The custom property value has been fixed. Now, how does the SettingItemView class obtain these values?

Call the initAttrs function in the constructor of the SettingItemView class, and then use the initAttrs function to return attributes. First, familiarize yourself with related APIs through some small practices.

/*** Return the property value of the custom attribute in the Attribute Set * @ param attrs: The Attribute Set maintained in the constructor */private void initAttrs (AttributeSet attrs) {// obtain the total number of attributes Log. I (tag, "attrs. getAttributeCount (): "+ attrs. getAttributeCount (); // obtain the attribute name and attribute value for (int I = 0; I <attrs. getAttributeCount (); I ++) {Log. I (tag, "name =" + attrs. getAttributeName (I); Log. I (tag, "value =" + attrs. getAttributeValue (I); Log. I (tag, "================================================================ = ");}}

After running the project, print the following log information in Logcat:

Explain some of the above Code:

The hexadecimal value of value = @ 2131427413 is 7F0B0055, which is actually in the corresponding R file.

public static final int siv_update=0x7f0b0055;

Others are simple.

Then we use other APIs to obtain the attribute values.

Private static final String NAMESPACE = "http://schemas.android.com/apk/res/com.wuyudong.mobilesafe ";........... /*** return the property value of the custom attribute in the Attribute Set * @ param attrs: The Attribute Set maintained in the constructor */private void initAttrs (AttributeSet attrs) {/* // obtain the total number of attributes Log. I (tag, "attrs. getAttributeCount (): "+ attrs. getAttributeCount (); // obtain the attribute name and attribute value for (int I = 0; I <attrs. getAttributeCount (); I ++) {Log. I (tag, "name =" + attrs. getAttributeName (I); Log. I (tag, "value =" + attrs. getAttributeValue (I); Log. I (tag, "================================================================ = ");} */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 running the project, print the following log information in Logcat:

Indicates that the set attribute value has been obtained successfully.

In this way, the code can be reused to implement the layout of the telephone location in the second column.

<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 = "auto update settings" mobilesafe: desoff = "auto update disabled" mobilesafe: deson = "auto update enabled"> </com. wuyudong. mobilesafe. view. settingItemView> <com. wuyudong. mobilesafe. view. settingItemView xmlns: mobilesafe = "http://schemas.android.com/apk/res/com.wuyudong.mobilesafe" android: layout_width = "match_parent" android: layout_height = "wrap_content" mobilesafe: destitle = "display settings of telephone locations" mobilesafe: desoff = "disabled" mobilesafe: deson =" enabled"> </com. wuyudong. mobilesafe. view. settingItemView>

 

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.