Getting Started with Android (iv) ui-creating a custom control

Source: Internet
Author: User

Original link: http://www.orlion.ga/441/

First, the introduction of layout

There is a title bar on the top of the iphone app that we can imitate to do, but if there are many activities in our program that need such a title bar, if each activity is written with a title bar will lead to code duplication, we can use the introduction of layout to solve the problem, Create a new layout title.xml. Code:

<?xml version= "1.0"  encoding= "Utf-8"? ><linearlayout xmlns:android= "http// Schemas.android.com/apk/res/android "    android:layout_width=" Match_parent "     android:layout_height= "Wrap_content" >    <Button     Android:id= "@+id/title_back"     android:layout_width= "Wrap_content"      android:layout_height= "wrap_content"     android:layout_gravity= "center"      android:layout_margin= "5dip"     android:text= "Back"      Android: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"      android:gravity= "Center" &Nbsp;   android:text= "Title text"     android:textcolor= "#fff"      android:textsize= "24SP"/><button    android:id= "@+id/title_edit"     android:layout_width= "wrap_content"     android:layout_height= "wrap _content "    android:layout_gravity=" center "    android:layout_margin= "5dip"     android:text= "Edit"     android:textcolor= "#fff"/></ Linearlayout>

Android:layout_margin This property can specify how far the control will be offset in the upper and lower left and right directions, or you can use Android:layout_marginleft or android:layout_ Properties such as MarginTop to specify the distance that the control is offset in a direction individually. Now that the title bar layout has been written, the rest is how to use the title bar in the program, and modify the code in the Activity_main.xml as follows:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Match_parent" android:layout_height= "Wrap_content" > <include layout= "@layout/title"/></linearlayout>

Ii. Creating a custom control

The title bar we created above has a return button that requires binding events. If you need to register again in each activity the click event of the return button adds a lot of duplicate code. This situation is best solved by using a custom control

The new titlelayout inherits from LinearLayout, making it our custom title bar control with the following code:

Package Com.example.uicustomviews;import Android.content.context;import Android.util.attributeset;import Android.view.layoutinflater;import Android.widget.linearlayout;public class Titlelayout extends LinearLayout {public Titlelayout (context context, AttributeSet Attrs) {Super (context, attrs); Layoutinflater.from (context). Inflate (R.layout.title, this);}}

First we rewrite the constructor with two parameters in LinearLayout, and the constructor is called when the Titlelayout control is introduced into the layout. The title bar is then dynamically loaded in the constructor, which is done with layoutinflate. The layoutinflate 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, the second parameter is to add a parent layout to the loaded layout, where we want to specify as Titlelayout, and then pass in this directly.

Now that the custom control has been created, then we need to add this custom control to the layout file and modify the code in Activity_main.xml as follows:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Match_parent" android:layout_height= "Wrap_content" > <ga.orlion.uicustomviews.titlelayout android:layout_width= "Match_par Ent "android:layout_height=" wrap_content "> </GA.ORLION.UICUSTOMVIEWS.TITLELAYOUT></LINEARLAYOUT&G T

The way to add custom controls and add controls is basically the same, except that when you add a custom control, we need to indicate the full class name of the control, and the package name cannot be ignored here.

Rerun the program and you can see the same effect as before.

Then register the Click event for the button in the title bar:

package com.example.uicustomviews;import android.content.context;import  android.util.attributeset;import android.view.layoutinflater;import android.view.view;import  android.widget.button;import android.widget.linearlayout;import android.widget.toast;public  Class titlelayout extends linearlayout {public titlelayout (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 (new  view.onclicklistener ()  {@Overridepublic  void onclick (view v)  {(( android.app.Activity)  getcontext ()). Finish ();}}); Titleedit.setonclicklistener (New view.onclicklistener ()  {@Overridepublic  void onclick (View &NBSP;V) &nbsP {Toast.maketext (GetContext ()  ,  "You clicked edit button"  , toast.length_ Short). Show ();}});}}

Getting Started with Android (iv) ui-creating 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.