Android fifth day---Create a custom control

Source: Internet
Author: User

1, imitation of the IPhone style, the top of the interface to place a title bar.

<?XML version= "1.0" encoding= "Utf-8"?><Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"    >    <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "50DP"Android:background= "#2197db"android:orientation= "Horizontal"Android:layout_alignparenttop= "true"Android:layout_alignparentleft= "true"Android:layout_alignparentstart= "true">        <ButtonAndroid:id= "@+id/title_back"Android:layout_width= "90DP"Android:layout_height= "40DP"android:layout_gravity= "Center"Android:layout_margin= "5DP"Android:text= "Back"            />        <TextViewAndroid:id= "@+id/title_text"Android:layout_width= "0DP"Android:layout_height= "Wrap_content"android:layout_gravity= "Center"Android:layout_weight= "1"android:gravity= "Center"Android:text= "title"Android:textcolor= "#fff"android:textsize= "24SP"            />        <ButtonAndroid:id= "@+id/title_edit"Android:layout_width= "90DP"Android:layout_height= "40DP"android:layout_gravity= "Center"Android:layout_margin= "5DP"Android:text= "OK"            />    </LinearLayout></Relativelayout>

The title bar layout has been written and the rest is how to use the title bar in your program.

<xmlns:android= "http://schemas.android.com/apk/res/android"android: Layout_width= "Match_parent"android:layout_height= "match_parent"> <  layout= "@layout/title"/></linearlayout > //We just need to introduce the title bar layout through a line of include statements. 

Then hide the system's own title bar in Mainactivity

 Public class extends Activity {@Overrideprotectedvoid  onCreate (Bundle savedinstancestate) {  Super. OnCreate (savedinstancestate); Requestwindowfeature (Window.feature_no_title); Setcontentview ( R.layout.activity_main);}}

We still need to write the event registration code separately for each of these controls in each activity. For example, the return button in the title bar, in fact, regardless of which activity, the function of the button is the same, that is, the destruction of the current activity, it is best to use a custom control way to solve.

To create a new custom title bar control:

 Public class extends linearlayout { public titlelayout (context context, AttributeSet attrs) {super   this);}}

/*

We rewrote the constructor with two parameters in LinearLayout, which is called when the Titlelayout control is introduced into the layout. It is then necessary to dynamically load the title bar layout in the constructor, which should be borrowed
Help Layoutinflater to achieve it. The Layoutinflater from () method allows you to build a Layoutinflater object and then call the inflate () method to dynamically load a layout file, and the inflate () method receives two parameters. The first parameter is the ID of the layout file to be loaded, here we pass in R.layout.title, and the second parameter is to add a parent layout to the loaded layout, where we want to specify as Titlelayout, and pass in this directly

*/

Add this custom control in the layout file

<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" ><Com.example.xxxxxx.TitleLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"></Com.example.xxxxxx.TitleLayout></LinearLayout>

Let's try registering a click event for the button in the title bar, modifying the code in Titlelayout

 Public classTitlelayoutextendsLinearLayout { 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 (NewOnclicklistener () {@Override Public voidOnClick (View v) {(Activity) GetContext ()). Finish ();        }        }); Titleedit.setonclicklistener (NewOnclicklistener () { Public Static FinalString TAG = ""; @Override Public voidOnClick (View v) {toast.maketext (GetContext (),"Rerun program", Toast.length_short). Show (); LOG.I (TAG,"111");    }        }); }}

Android fifth day---Create a custom control

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.