The advantages of this approach are simple, easy to understand and suitable for developing custom UI controls that are infrequently used
The disadvantage is that the comparison is not flexible, if other applications want to use this control, you have to change a lot
In short, this method is used to make the finished product, the next method is used to do the template.
First look at the finished product, this is a title bar control:
Consists of the left and right two buttons and a textview:
Implementation method:
The first step: Define an XML file to design your own prototype of your custom control
Example code: File name is title
1<?xml version= "1.0" encoding= "Utf-8"?>2<LinearLayout3Xmlns:android= "Http://schemas.android.com/apk/res/android"4Android:layout_width= "Match_parent"5android:layout_height= "Wrap_content"6android:background= "#000" >7<Button8Android:id= "@+id/title_back"9Android:layout_width= "Wrap_content"Tenandroid:layout_height= "Wrap_content" Oneandroid:layout_gravity= "Center" AAndroid:layout_margin= "5dip" -Android:background= "#000" -Android:text= "Back" theAndroid:textcolor= "#fff"/> -<TextView -Android:id= "@+id/title_text" -Android:layout_width= "0dip" +android:layout_height= "Wrap_content" -android:layout_gravity= "Center" +android:layout_weight= "1" Aandroid:gravity= "Center" atandroid:text= "Title text" -Android:textcolor= "#fff" -Android:textsize= "24sp"/> -<Button -Android:id= "@+id/title_edit" -Android:layout_width= "Wrap_content" inandroid:layout_height= "Wrap_content" -android:layout_gravity= "Center" toAndroid:layout_margin= "5dip" +Android:background= "#000" -android:text= "Edit" theAndroid:textcolor= "#fff"/> *</LinearLayout>
Next, if you don't have to set a response event, write the code directly in the layout file that you need to reference the control.
<include layout= "@layout/title"/>
1 <2 3 android:layout_width= "Match_parent" android:layout_height= "Match_parent" >4 <include layout= "@layout/title"/>5 </LinearLayout>
If you want to set up a response event, proceed to the next step
Step two: Create a Topbar class to inherit the layout class and set the response event by getting the ID of the control that needs to set the response event in the title layout file.
Example code:
1 Public classTitlelayoutextendsLinearLayout {2 3 PublicTitlelayout (Context context, AttributeSet attrs) {4 Super(context, attrs);5Layoutinflater.from (context). Inflate (R.layout.title, This); 6Button Titleback =(Button) Findviewbyid (r.id.title_back);7Button Titleedit =(Button) Findviewbyid (r.id.title_edit);8Titleback.setonclicklistener (NewOnclicklistener () {9 @OverrideTen Public voidOnClick (View v) { OneToast.maketext (GetContext (), "You clicked Back button", A toast.length_short). Show (); - } - }); theTitleedit.setonclicklistener (NewOnclicklistener () { - - @Override - Public voidOnClick (View v) { +Toast.maketext (GetContext (), "You clicked Edit button", - toast.length_short). Show (); + } A }); at } -}
Part III: This is set in the layout file:
1 < 2 3 4 android:layout_height= "Match_parent" > 5 < 6 7 android:layout_height= "Wrap_content" > 8 </com.example.diy.TitleLayout> 9 </LinearLayout>
All the required functions are completed.
Note that the control needs to write the full package name!
If there is any mistake, or I understand wrong or improper, I implore everyone to correct, thank you! Hey, hehe.
Android Custom UI controls (simple and easy version, but not flexible)