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