Android Learning seven (creating custom controls)

Source: Internet
Author: User

The basic controls and layouts used for Android are learned, but the basic controls and layouts do not sometimes implement complex layouts. Let's look at the relationships between the various controls and layouts.



It can be seen that all controls are directly or indirectly inherited from view, and all layouts are directly or indirectly based on self-viewgroup. View is one of the most basic UI components in Android, it can draw a rectangular area on the screen and can respond to various events in this area, so the various controls we use actually add a variety of unique features to the view. ViewGroup is a special view that can contain a lot of view and sub-viewgroup, which is a container for placing controls and layouts. There are two simple ways to create a custom control, one is to introduce a layout, and the other is to customize the control.

First, create a project whose name is Uicustomviews.

Let's define a title bar ourselves.

Create a new layout with the following 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 "android:orientation=" Horizontal "Android:background=" @drawable/title_bg "> <button android:id=" @+id/title_back "Android:la Yout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "center" Androi D:layout_margin= "5dip" android:background= "@drawable/back_bg" 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:gravit        y= "center" 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:la yout_gravity= "center" android:layout_margin= "5dip" android:background= "@drawable/edit_bg" Android:tex t= "Edit" android:textcolor= "#fff"/> </LinearLayout>


To modify the main layout, the code is as follows:

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



Modify the OnCreate method inside the Mainactivity

The code is as follows:

@Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Requestwindowfeature (Window.feature_no_title); Setcontentview (R.layout.activity_main);}


<include layout= "@layout/title"/> Is the introduction of a layout file

Requestwindowfeature (Window.feature_no_title); is to hide the system's own title bar

The results of the program run as follows:



Using this method of introducing layout, no matter how many layouts you need to add a title bar, just one line of the include statement.


The technique of introducing layouts does solve the problem of repeating code, but if there are controls in the layout that require the ability to respond to events, we still need to write the event registration code separately for each of these controls in each activity. But the code is the same, so this brings in a lot of duplicate code, and then the custom control.


Custom controls


The new titlelayout inherits LinearLayout, and the code and description are as follows:

Package Com.wj.uicustomviews;import Android.app.activity;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);//TODO Auto-generated Constructor stub/* * Overrides a constructor with 2 parameters in LinearLayout, which is called when a Titlelayout control is introduced into the layout * The title bar layout needs to be dynamically loaded in the constructor, which is accomplished with the help of Layoutinflater. by Layoutinflater the * FROM () method you can build a Layoutinflater object, and then call the inflate () method to dynamically load a layout file, inflate method * Receives 2 parameter number, The first parameter is the ID of the layout to be loaded, here we pass in R.layout.title, the second parameter is to add a parent layout to the loaded layout, * Here we want to designate as Titlelayout, so pass in this * */ Layoutinflater.from (context). Inflate (R.layout.title, this);//Event Handling button titleback= (button) Findviewbyid ( R.id.title_back); Button titleedit= (button) Findviewbyid (R.id.title_edit); Titleback.setonclicklistener (new Onclicklistener () {@ Overridepublic void OnClick (View v) {//TODO auto-generated Method Stub (Activity) GetContext ()). Finish ();}); Titleedit.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated method Stubtoast.maketext (GetContext (), "You clicked Edit Button", Toast.length_short). Show ();}});}}


In the layout file, reference the custom control to modify the main layout file, as follows:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    >    <!--<include layout= "@layout/title"/>--    <com.wj.uicustomviews.titlelayout        android:layout_width= "match_parent"        android:layout_height= "wrap_content"        >            </ Com.wj.uicustomviews.titlelayout></linearlayout>


The results of the operation are as follows:




Click the button to have an appropriate event response.

Note that when you add a custom control, you need to indicate the full package name and class name


Reprint Please specify: http://blog.csdn.net/j903829182/article/details/40682583



Android Learning seven (creating custom controls)

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.