Android first line of code-6. Custom Control Implementation

Source: Internet
Author: User

0. Suppose that the title bar control in an app is shared, and if each activity needs to set the button, the code is bloated. Then we can customize the control and then inherit the control on the line.

Custom controls are Titlelayout,layoutinflater.from (context). Inflate (r.layout.title,this); dynamic loading can be implemented. Layoutinflater is used to find the XML layout file under Res/layout/, and is instantiated, similar to Findviewbyid (), but Findviewbyid is looking for specific widget controls under the XML layout file (such as button, TextView, etc.).

1. Control implementation

 Public classTitlelayoutextendsLinearLayoutImplementsView.onclicklistener { Publictitlelayout (Context context,attributeset attrs) {Super(context, attrs); Layoutinflater.from (context). Inflate (R.layout.title, This); Button Titleback=(Button) Findviewbyid (r.id.title_back); Button Titleedit=(Button) Findviewbyid (R.id.title_edit); Titleback.setonclicklistener ( This); Titleedit.setonclicklistener ( This); } @Override Public voidOnClick (View v) {Switch(V.getid ()) { Caser.id.title_back: (Activity) GetContext ()). Finish ();  Break;  CaseR.id.title_edit:toast.maketext (GetContext (),"You click this button", Toast.length_short). Show ();  Break; }    }}

2. Control Reference

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Android:weightsum= "1">        <Activitytest.example.com.uiwidgettest.TitleLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"></Activitytest.example.com.uiwidgettest.TitleLayout></LinearLayout>

Android first line of code-6. Custom Control Implementation

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.