Inherit ViewGroup Research (1)-introduction and a small Demo

Source: Internet
Author: User

Another new chapter has been opened. Haha, what I learned last time was to inherit the View. The personal feeling of inheriting the View is not so perfect. What I do is always want to make everything perfect, but there is no perfect world. This is the ViewGroup study. To tell the truth, I personally feel that the functions of this class are still very powerful. Here we only provide the most basic things. Well, let's continue to study it. The road is long and it takes a long time to complete the study, I will try again and again.

 

I,ViewGroupOverview

 

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. This class also defines the LayoutParams class to specify the Layout parameters of this base class. (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.

 

II,ViewGroupThree methods

 

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

 

1,OnLayoutMethod

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 is used in our layout and will be used later.

 

2,AddViewMethod

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,GetChildAtMethod

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 we customizeViewGroupThese three methods are crucial. Let's take a look at the customViewGroup.

 

3. A smallDemo

 

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 context, AttributeSet attrs ){
Super (context, attrs );
// TODO Auto-generated constructor stub
}

Public HelloViewGroup (Context 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:

 

 

Is it effective? haha, try it by yourself, but remember to create a mContext and initialize it in the constructor before.

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.