Three ways to implement Android custom layouts

Source: Internet
Author: User

In the completion of the project to use a number of custom layout, has been intended to summarize the implementation of the custom layout, today to summarize it. Prior to this, I learned a few blog posts on the Guo Lin blog about custom view, and I have benefited a lot from this article.

In summary, there are three ways to implement a custom layout, namely, a composite control, a self-drawing control, and an inherited control. These three methods are described separately below.

(i) Composite controls

The combination of controls, as the name implies, is the combination of small controls to form a new control, which is mostly the system's own control. For example, many applications commonly used in the title bar control, its practical is the combination of controls, then the following will be implemented by a simple title bar custom control to say the use of composite controls.

1. Create a new Android project, creating a custom title bar layout file Title_bar.xml:

1<?XML version= "1.0" encoding= "Utf-8"?>2<RelativelayoutXmlns:android= "Http://schemas.android.com/apk/res/android"3Android:layout_width= "Match_parent"4Android:layout_height= "Wrap_content"5Android:background= "#0000ff">67<Button8Android:id= "@+id/left_btn"9Android:layout_width= "Wrap_content"10Android:layout_height= "Wrap_content"11Android:layout_centervertical= "true"12Android:layout_margin= "5DP"13Android:background= "@drawable/back1_64"/>1415<TextView16Android:id= "@+id/title_tv"17Android:layout_width= "Wrap_content"18  android:layout_height = "wrap_content" 19 = "true" 20  Android:text= "This is the title" 21  Android:textcolor=" #ffffff " 22  Android:textsize= " 20SP "/>23 24 </relativelayout>     

Visible this title bar control is still relatively simple, in which there is a return button on the left, the background is a prepared picture back1_64.png, the title bar is the title text in the middle.

2, create a class Titleview, inherit from Relativelayout:

1  Public classTitleviewextendsRelativelayout {2 3     //returns a Button control4     PrivateButton mleftbtn;5     //Title TV6     PrivateTextView Mtitletv;7 8      PublicTitleview (Context context, AttributeSet attrs) {9         Super(context, attrs);Ten  One         //Loading Layouts ALayoutinflater.from (context). Inflate (R.layout.title_bar, This); -  -         //Get Control theMLEFTBTN =(Button) Findviewbyid (R.ID.LEFT_BTN); -Mtitletv =(TextView) Findviewbyid (R.ID.TITLE_TV); -  -     } +  -     //Add a custom Click event for the left return button +      Public voidSetleftbuttonlistener (Onclicklistener listener) { A Mleftbtn.setonclicklistener (listener); at     } -  -     //How to set a caption -      Public voidSettitletext (String title) { - Mtitletv.settext (title); -     } in}

In Titleview, the layout is loaded primarily for a custom title bar, an event listener method is added for the Back button, and a method for setting the caption text is provided.

3. Introduce a custom title bar in Main_activity.xml:

1 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Android:id= "@+id/main_layout"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 android:orientation= "vertical" >6 7     <Com.example.test.TitleView8         Android:id= "@+id/title_bar"9 Android:layout_width= "Match_parent"Ten Android:layout_height= "Wrap_content" > One     </Com.example.test.TitleView> A  - </LinearLayout>

4. Get the custom title bar in Mainactivity and add a custom click event for the Back button:

1        PrivateTitleview Mtitlebar;2Mtitlebar =(Titleview) Findviewbyid (R.id.title_bar);3 4Mtitlebar.setleftbuttonlistener (NewOnclicklistener () {5 6 @Override7              Public voidOnClick (View v) {8Toast.maketext (mainactivity. This, "Click the Back button", Toast.length_short)9 . Show ();Ten finish (); One             } A});

In this way, the custom title bar is implemented in a combination, and more complex custom controls, such as custom search bars, can be created through more combinations.

(ii) Self-drawing controls

refer:http://blog.csdn.net/guolin_blog/article/details/17357967

Three ways to implement Android custom layouts

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.