Recommended reading: An analysis of Android phone defender turn off automatic Updates
The custom composite control completed in the previous section is not flexible enough to display information about the control, and to customize its own properties, modeled on the system properties
The previous section combines control Settingitemview with three controls, which are textview headings, textview descriptions, checkbox check boxes
Custom Properties Tsh:title= "big title" and "Tsh:desc_on=", tsh:desc_off= "small title off"
Add namespaces, xmlns:tsh= "http://schemas.android.com/apk/res/package name"
Creating Attrs.xml files in the res/values/directory
Add node <declare-styleable name= "TextView" >
Add nodes under node <attr name= "title" format= "string"/>, add nodes of two other properties
When a layout file is used, a construction method with two parameters is invoked
Within this construct method, a AttributeSet object is passed
Call the AttributeSet object's Getattributevalue () method, get the property value, parameter: index position, not recommended
Invokes the Getattributevalue (Namespace,name) method of the AttributeSet object, Parameter: namespace, property name
Invokes the SetText () method of the TextView object, directly to the set in
Description section, in the Setchecked () method, judge, and then set
Settingitemview.java
Package com.qingguow.mobilesafe.ui;
Import Android.content.Context;
Import Android.util.AttributeSet;
Import Android.view.View;
Import Android.widget.CheckBox;
Import Android.widget.RelativeLayout;
Import Android.widget.TextView;
Import COM.QINGGUOW.MOBILESAFE.R; public class Settingitemview extends Relativelayout {private TextView tv_title; private TextView tv_desc; private CHECKBO
x cb_status;
Private String desc_on;
Private String Desc_off; /** * Initialize View Object * @param context/private void Initview (context) {view.inflate, R.layout.setting_item_v
Iew, this);
Cb_status= (CheckBox) This.findviewbyid (r.id.cb_status);
Tv_desc= (TextView) This.findviewbyid (R.ID.TV_DESC);
Tv_title= (TextView) This.findviewbyid (r.id.tv_title); /** * To determine whether to select * @return/public boolean ischecked () {return cb_status.ischecked ();}/** * setting is selected * @param status/PU Blic void setchecked (Boolean status) {if (status) {Tv_desc.settext (desc_on);} else{Tv_desc.settext (Desc_off);} cb_status.setchecked (status); /** * Set Display text * @param text */public void Setdesc (String text) {tv_desc.settext (text);} public Settingitemview (context Context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle); Initview (context);} public Settingitemview ( Context, AttributeSet Attrs} {Super (context, attrs), Initview (context); Gets the passed property String title=attrs.getattributevalue ("Http://schemas.android.com/apk/res/com.qingguow.mobilesafe", "
Title ");
Tv_title.settext (title);
Desc_on=attrs.getattributevalue ("Http://schemas.android.com/apk/res/com.qingguow.mobilesafe", "desc_on");
Desc_off=attrs.getattributevalue ("Http://schemas.android.com/apk/res/com.qingguow.mobilesafe", "Desc_off"); Settingitemview (context) {super (context), initview (context);}
Activity_setting.xml
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
xmlns:tsh=" Http://schemas.android.com/apk/res/com.qingguow.mobilesafe "
android:layout_ Width= "Match_parent"
android:layout_height= "match_parent"
android:orientation= "vertical" >
< TextView
android:layout_width= "match_parent"
android:layout_height= "40DP"
android:background= "# CCC "
android:gravity=" center "
android:text=" setting centre "
android:textsize=" 20sp "/>
< Com.qingguow.mobilesafe.ui.SettingItemView
tsh:title= "Set Automatic Updates"
tsh:desc_on= "set Automatic Update on"
TSH:DESC_ off= "Set Automatic update shutdown"
android:layout_width= "wrap_content"
android:layout_height= "Wrap_content"
Android : id= "@+id/siv_item" >
</com.qingguow.mobilesafe.ui.SettingItemView>
Attrs.xml
<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
<declare-styleable name= "TextView" >
<attr name= "title" format= "string"/>
<attr name= "desc_on" format= "string"/>
<attr "Desc_off" format= "string"/>
</declare-styleable>
</resources>
The above is for the Android phone defender custom control properties related to the introduction, I hope to help!