Custom viewgroup in Android

Source: Internet
Author: User

1. viewgroup Overview

 

Before studying viewgroup, let's take a look at the introduction of viewgroup:

/**

* A viewgroup is a special view that can contain in other views

* (Called children.) The view group is the base class for layouts and views

* Containers. This class also defines

* Android. View. viewgroup. layoutparams class which serves as the base

* Class for layouts parameters.

A viewgroup is a special view that can contain other views. viewgroup is the base class of each layout and view component. (The translation is not very good. You can understand it)

 

Android still has a clear explanation of viewgroup. Here we can see the following points:

1. viewgroup is a container that inherits from the view.

2. viewgroup is a base class of layout and some view components.

And so on. If you have a high vision, believe that you can see how far it is.

 

2. Three Methods of viewgroup

 

There are three important methods to inherit viewgroup. Let's take a look at them below:

 

1. onlayout Method

Protected void onlayout (Boolean changed, int left, int top, int right, int bottom ){

}

This method is provided in addition to the constructor when we inherit viewgroup. We can see that the method is defined in the source code of viewgroup, that is, the parent class does not provide the method content, we need to implement it ourselves.

This method is called when the view needs to allocate the size and position of all sub-objects.

 

2. addview Method

Public void addview (view child ){
Addview (child,-1 );
}

This method is used to add components to the view container. We can use this method to add components to this viewgroup.

 

3. getchildat Method

Public View getchildat (INT index ){
Try {
Return mchildren [Index];
} Catch (indexoutofboundsexception ex ){
Return NULL;
}
}

This method is used to return the view at the specified position.

Note: Views in viewgroup are counted from 0.

 It can be said that these three methods are crucial for custom viewgroup. Let's take a look at the usage of custom viewgroup.

 

3. A small demo

 

Create a project named androidviewgroup, and the activity name is mainactivity. Write a class inherited from viewgroup named helloviewgroup.

--> Helloviewgroup class

Public class helloviewgroup extends viewgroup {

Public helloviewgroup (context, attributeset attrs ){
Super (context, attrs );
// Todo auto-generated constructor stub
}

Public helloviewgroup (context ){
Super (context );
// Todo auto-generated constructor stub
}

@ Override
Protected void onlayout (Boolean changed, int L, int T, int R, int B ){
// Todo auto-generated method stub

}

}

 

--> Mainactivity class

Public class mainactivity extends activity {
/** Called when the activity is first created .*/
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (New helloviewgroup (this ));
}
}

 

Then you can run it and find that the label on the screen except the status bar is black. Next we will modify the code to make our viewgroup fire.

Create a new method named myaddview. This method is used to add components to viewgroup:

 

/**
* Add view Method
**/
Public void myaddview (){
Imageview micon = new imageview (mcontext );
Micon. setimageresource (R. drawable. Haha );
Addview (micon );
}

 

Then we modify the onlayout method:

@ Override
Protected void onlayout (Boolean changed, int L, int T, int R, int B ){
View v = getchildat (0 );
V. layout (L, t, R, B );
}

 

Then let's take a look at the running effect:

 

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.