Android Development Learning note-Custom control properties

Source: Internet
Author: User

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

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.