Custom View and ViewGroup

Source: Internet
Author: User

The View class is the parent class of ViewGroup. ViewGroup has all the features of View. ViewGroup is mainly used to act as the View container and manage the View as its own child, of course, the child can also be of the ViewGroup type. The View class is generally used for drawing operations. It overrides its onDraw method, but it cannot contain other components and does not have the addView (View view) method. ViewGroup is a component container that can contain any component, but must override onLayout (boolean changed, int l, int t, int r, int B) and onMesure (int widthMesureSpec, int heightMesureSpec) method. otherwise, components added to ViewGroup are not displayed. <Strong> <span style = "font-family: FangSong_GB2312; font-size: 14px;"> package com. example. testrefreshview; import android. app. activity; import android. content. context; import android. OS. bundle; import android. view. viewGroup; import android. widget. button; import android. widget. textView; public class MainActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onC Reate (savedInstanceState); setContentView (new MyViewGroup (this);} public class MyViewGroup extends ViewGroup {public MyViewGroup (Context context) {super (context ); button button1 = new Button (context); button1.setText ("button1"); Button button2 = new Button (context); button2.setText ("button2 "); textView textView = new TextView (context); textView. setText ("textView"); addView (button1); addView (bu Tton2); addView (textView) ;}@ Override protected void onLayout (boolean arg0, int arg1, int arg2, int arg3, int arg4) the layout (int left, int top, int right, int bottom) method of </span> </strong> View is used to place the view in the specified position of the parameter, therefore, if we traverse each sub-view in the Custom ViewGroup: onLayout and use the view. layout () specifies its location, and each sub-View calls onLayout again. this constitutes a recursive call process. If you override the onDraw method in ViewGroup, you need to call this in the constructor. setWillNoDraw (flase); in this case, the system calls the overwritten onDraw (Canvas canc) As) method, otherwise the system will not call the onDraw (Canvas canvas) method. Modify the above Code to display it. <Strong> <span style = "font-family: FangSong_GB2312; font-size: 14px;"> package com. example. testrefreshview; import android. app. activity; import android. content. context; import android. OS. bundle; import android. view. view; import android. view. viewGroup; import android. widget. button; import android. widget. textView; public class MainActivity extends Activity {@ Override protected void onCreate (Bundle saved InstanceState) {super. onCreate (savedInstanceState); setContentView (new MyViewGroup (this);} public class MyViewGroup extends ViewGroup {public MyViewGroup (Context context) {super (context ); button button1 = new Button (context); button1.setText ("button1"); Button button2 = new Button (context); button2.setText ("button2 "); textView textView = new TextView (context); textView. setText ("textView"); ad DView (button1); addView (button2); addView (textView) ;}@ Override protected void onLayout (boolean arg0, int arg1, int arg2, int arg3, int arg4) {int childCount = getChildCount (); int left = 0; int top = 10; for (int I = 0; I <childCount; I ++) {View child = getChildAt (I); child. layout (left, top, left + 60, top + 60); top + = 70 ;}}</span> </strong> let's look at the code again: <strong> <span style = "font-family: Fang Song_GB2312; font-size: 14px; "> @ Override protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {int childCount = getChildCount (); // set the ViewGroup size int specSize_width = MeasureSpec. getSize (widthMeasureSpec); int specSize_height = MeasureSpec. getSize (heightMeasureSpec); setMeasuredDimension (specSize_width, specSize_height); for (int I = 0; I <childCount; I ++) {View childView = getC HildAt (I); childView. measure (80, 80) ;}</span> </strong> by overwriting onMeasure, you can not only specify the size of ViewGroup, but also specify the size of each sub-View by traversing, add the preceding code to the custom ViewGroup to assign the display width and height to each sub-View in the ViewGroup. Let's move the sub-View. Add the following code: <strong> <span style = "font-family: FangSong_GB2312; font-size: 14px;"> public boolean onTouchEvent (MotionEvent ev) {final float y = ev. getY (); switch (ev. getAction () {case MotionEvent. ACTION_DOWN: mLastMotionY = y; break; case MotionEvent. ACTION_MOVE: int detaY = (int) (mLastMotionY-y); mLastMotionY = y; scrollBy (0, detaY); break; case MotionEvent. ACTION_UP: break;} return tr Ue ;}</span> </strong> A scrollBy method is used above. Open the official API and you can see that the View class has the following two methods: both functions seem to be mobile views, so what are their differences? With this question, let's look at it. First, we must understand that the Android View has no boundaries and the Canvas has no boundaries, however, when we draw a specific View, we perform some operations on the Canvas object, such as: translate (translation), clipRect (CUT), etc, in order to meet our requirements for drawing the Canvas object, we can call this borderless view as "view coordinate"-it is not restricted by the physical screen. Normally, a Layout file is only the display area of the view. If it is exceeded, it cannot be displayed in the area of the parent view, we can call this boundary View "layout coordinates" ------ layout size allocated by the parent View to the Child view. In addition, the starting coordinate of a view on the screen is located at the starting point of the view coordinate, as shown in. Because the layout coordinates can only display a specific part of the content, we can only move the coordinates of the layout coordinates to display any position of the View coordinates. <Strong> <span style = "font-family: FangSong_GB2312; font-size: 14px;"> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: orientation = "vertical" android: background = "#888888"> <TextView android: id = "@ + id/txt" android: layout_width = "300dip" android: layout_height = "120dip" android: background = "# cccccc" android: text = "textview"/> <Button android: id = "@ + id/btn" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "button"/> </LinearLayout> </span> </strong>

Related Article

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.