If you want to make your custom control more convenient and flexible, you need to define the properties of the control to make it easier to use.
Here's how to customize the properties of a control
1, add attrs.xml, content format style can refer to Sdk\platforms\android-10\data\res\values\attrs.xml file
<?XML version= "1.0" encoding= "Utf-8"?><Resources> <declare-styleablename= "Compoundbutton"> <attrname= "Desc_title"format= "string" /> <attrname= "desc_on"format= "string"/> <attrname= "Desc_off"format= "string"/> </declare-styleable></Resources>
2. Reference the namespace in the layout file and set the custom property value, where the name of the application package after the named control
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:frank= "Http://schemas.android.com/apk/res/com.frank.mobilesafe"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical" > <TextViewAndroid:id= "@+id/tv_maintitle"Android:layout_width= "Match_parent"Android:layout_height= "55DP"Android:background= "#8866ff00"android:gravity= "Center"Android:text= "Set Center"android:textsize= "22SP" /> <Com.frank.mobilesafe.ui.SettingItemViewFrank:desc_title= "New version upgrade Control"frank:desc_on= "Upgrade with new version"Frank:desc_off= "Do not Upgrade"Android:id= "@+id/siv_update"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content" /></LinearLayout>
3. The property value is obtained or set in the construction method inside the class of the custom control.
Public Settingitemview (Context context, AttributeSet attrs) { Super(context, attrs); Initview (context); = "Http://schemas.android.com/apk/res/com.frank.mobilesafe"; = Attrs.getattributevalue (mynamespace, "Desc_title"); = Attrs.getattributevalue (mynamespace, "desc_on"); = Attrs.getattributevalue (mynamespace, "Desc_off"); Tv_update_title.settext (Str_desc_title); }
4. Other uses of custom attribute values can be handled
/*** Set the status of the combined control * *@paramisChecked*/ Public voidSetchecked (BooleanisChecked) {cb_update.setchecked (isChecked); if(isChecked) {Setdesc (str_desc_on); } Else{Setdesc (Str_desc_off); } } /*** Set Description information * *@paramisChecked*/ Public voidSetdesc (String text) {tv_update_content.settext (text); }
Android Development Learning note-Custom control properties