Android Custom Combo Control Summary

Source: Internet
Author: User

Introduction

With the development of the Android UI since the time, the custom composite control has a certain understanding, to summarize, the summary of this article is mainly about how to use the Android SDK provides the layout and controls to form a functional complete composite control and encapsulate it as an object-oriented class, Instead of discussing how to inherit from the control classes provided by the SDK (such as TextView), it is a matter of customizing the extension.

Before we get to the point, let's start by looking at a set of functional requirements

Suppose in the mobile phone demand, so as the above three interface we can use three activity, each activity a layout file, the implementation is relatively independent, but assume on Android pad requires as above three interface on a dialog box, and the transition process to have a gradient animation, So how do we do that? My plan is to implement it by using a floating activity plus viewflipper. Although we can get control of each interface in this floating activity class and add the corresponding operation, but if put all in an activity class, it will make the code in this class is very large, the code is not easy to go to the days, and also less OO concept, To do this, consider using a custom composite control class for each interface. In the same vein, assuming that the first interface on the top of the list interface for Weibo binding, we can consider using the ListView control plus a custom extension of adapter, each of which is actually a composite control that needs to be created in our custom adapter. To try to customize the use of the combined controls, I did not use the ListView for the list of tweets, but instead used the basic addview approach.

To customize a composite control

The following are steps to illustrate the implementation and encapsulation of custom composite controls:

Create and write a layout for a composite control

This step is the same as other methods of creating and writing layout resources, you can use the drag control, you can also use code input, and other third-party development tools can be written, in short, the result is to get a layout to meet the needs, for the convenience of the following, I take the second interface layout account_info _preview.xml part of the code to illustrate the following

<?xml version= "1.0" encoding= "Utf-8"?>

<linearlayout

Xmlns:android= "Http://schemas.android.com/apk/res/android"

android:orientation= "Vertical"

Android:layout_width= "Fill_parent"

android:layout_height= "Fill_parent"

android:background= "@drawable/login_view_back" >

<relativelayout

android:layout_height= "@dimen/content_top_land_height"

Android:layout_width= "Fill_parent"

android:padding= "@dimen/login_title_padding"

android:background= "@drawable/login_title_back" >

......

</RelativeLayout>

</LinearLayout>

The implementation of this step is required by the WYSIWYG principle, so that after the layout of the graphical layouts in the interface to see the effect of the interface you want to implement the static effect on it.

New custom class for a composite control

Above the Account_info_ Preview.xml The layout file is created, you can create a custom class that you need for the layout of the combo control, assuming that the class is named the Accountinfopreview class, the class is created first through Eclipse, and the parent class of the class is the class that corresponds to the combined control's root element, which is Linea Rlayout, so this accountinfopreview should be derived from linearlayout.

LinearLayout has two constructors, in the Accountinfopreview class we have to rewrite the constructor of the parent class, because in use we do not use the direct new Accountinfopreview (context context) Way to create the combined control, so we just need to overload a public accountinfopreview (context context, AttributeSet Attrs) constructor, which depends on the requirements of your custom combo control.

In addition, in order to instantiate a control object in a custom class, we need to override the protected void Onfinishinflate () function to achieve a one by one correspondence between the resource ID and the control object. The following is an example of pasting code

Package Com.netease.pris.hd.view;

......

public class Accountinfopreview extends LinearLayout

{

TextView Mviewtitle;

ImageView Mheader;

Public Accountinfopreview (context context, AttributeSet attrs)

{

Super (context, attrs);

TODO auto-generated Constructor stub

}

@Override

protected void Onfinishinflate ()

{

Mviewtitle = (TextView) Findviewbyid (R.id.aip_title_text);

Mheader = (ImageView) Findviewbyid (r.id.aip_header_imgae);

}

......

}

As above, for the custom control class to set up the framework, the specific need to do what to do, you can write to it, such as adding control click events and so on.

Modifying a composite control layout root element for a custom class

The custom composite control's class has, and the layout of the custom composite control also has a one by one instantiation of the controls inside the combo control with Findviewbyid, but the entire combination control class and layout have not yet established an association, this step is done by using the package name of the custom class + The class name is done to modify the root element of the custom composite control layout. We see the package name for the custom class above is Com.netease.pris.hd.view, the class name is Accountinfopreview, so we re-put the above account_info_ The LinearLayout root element in the Preview.xml is replaced with Accountinfopreview. The following example

<?xml version= "1.0" encoding= "Utf-8"?>

<com.netease.pris.hd.view.accountinfopreview

Xmlns:android= "Http://schemas.android.com/apk/res/android"

android:orientation= "Vertical"

Android:layout_width= "Fill_parent"

android:layout_height= "Fill_parent"

android:background= "@drawable/login_view_back" >

<relativelayout

android:layout_height= "@dimen/content_top_land_height"

Android:layout_width= "Fill_parent"

android:padding= "@dimen/login_title_padding"

android:background= "@drawable/login_title_back" >

......

</RelativeLayout>

</com.netease.pris.hd.view.AccountInfoPreView>

Using custom composite controls

Well, the steps to create a custom composite control are described above, and a Accountinfopreview custom composite control is created, and then we'll explain the use of this custom composite control.

Depending on the static and dynamic loading of the resources, the use of custom composite controls, there are also static and dynamic two ways, in fact, is also very simple two ways, and the Android inflate is related.

Static loading

The static load here is that the custom combo control is used to include the layout of the XML resource, and this layout is set in by the activity's Setconentview () function. Then we can instantiate the object of the custom composite control in a Findviewbyid way.

For example, the Accountinfopreview custom combo control created above is included in the layout account_view_flipper.xml of the activity's Setconentview function, and its contents are as follows:

<?xml version= "1.0" encoding= "Utf-8"?>

<linearlayout

Xmlns:android= "Http://schemas.android.com/apk/res/android"

android:orientation= "Vertical"

Android:layout_width= "@dimen/login_dialog_width"

android:layout_height= "@dimen/login_dialog_height" >

<viewflipper

Android:id= "@+id/account_view_flippe"

Android:layout_width= "Fill_parent"

android:layout_height= "Fill_parent" >

<include

Android:id= "@+id/account_info_preview"

layout= "@layout/account_info_preview"/>

</ViewFlipper>

</LinearLayout>

We can instantiate the object of the custom composite control by Findviewbyid method, the code is as follows

Accountinfopreview Maipview = (accountinfopreview) Findviewbyid (R.id.account_info_preview);

The object was successfully instantiated, and the specific use of the object later is not listed.

Dynamic loading

Dynamic loading, that is, the custom composite control is not included in the resource layout, and we are dynamically created in the code to the desired viewgroup. Because a custom composite control, unlike a view of an underlying control such as TextView, or a custom non-associated resource, cannot be created directly through the new method, you need to instantiate the custom composite control object with the Inflate method and add it to the ViewGroup. We can rewrite the above layout account_view_flipper.xml file as

<?xml version= "1.0" encoding= "Utf-8"?>

<linearlayout

Xmlns:android= "Http://schemas.android.com/apk/res/android"

android:orientation= "Vertical"

Android:layout_width= "@dimen/login_dialog_width"

android:layout_height= "@dimen/login_dialog_height" >

<viewflipper

Android:id= "@+id/account_view_flippe"

Android:layout_width= "Fill_parent"

android:layout_height= "Fill_parent" >

</ViewFlipper>

</LinearLayout>

In the OnCreate function of activity we can write code like this

public void OnCreate (Bundle savedinstancestate)

{

Super.oncreate (savedinstancestate);

Setcontentview (R.layout.account_view_flipper);

Viewflipper Mviewflipper = (viewflipper) Findviewbyid (R.id.account_view_flippe);

Accountinfopreview Maipview = (accountinfopreview) getlayoutinflater (). Inflate (R.id.account_info_preview, NULL);

Mviewflipper.addview (Maipview);

}

With the code above, we also instantiate a Accountinfopreview control object and dynamically load it onto the interface for display. The specific operation is also based on the specific needs to achieve, here do not expand.

About the Android custom Combo control on the summary here, if the text is not correct in the place also hope to help treatise, thank you.

Android Custom Combo Control Summary

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.