What is the difference between view and ViewGroup?

Source: Internet
Author: User

Baidu know: Http://zhidao.baidu.com/link?url=B5MFOzDlww8soYqr5CL5FldH4sXD6eumS1XTRn8XEh8gu4mKjQdPkJSLIBt7u_ Xtf7jcrjrjegwiqcweq1z-nq

The UI interface for Android is made up of a combination of view and ViewGroup and their derived classes.
where view is the base class for all UI components, ViewGroup is the container that holds the components, which itself is derived from view.
The View object is the base unit for the user interface in the Android platform.
The view class is the basis of a subclass of what it calls "widgets (Tools)", which provide a complete implementation of UI objects such as text input boxes and buttons.
The ViewGroup class also lays the groundwork for its subclasses, called Layouts (Layout), which provide layout schemas such as streaming layouts, table layouts, and relative layouts.
In general, the UI interface for developing Android applications does not use view and viewgroup directly, but instead uses the derived classes of these two large base classes.
the immediate subclasses derived from view are: Analogclock,imageview,keyboardview, Progressbar,surfaceview, Textview,viewgroup,viewstub
the indirect subclasses derived from view are: Abslistview,absseekbar, Absspinner, Absolutelayout, Adapterview,adapterviewanimator, Adapterviewflipper, Appwidgethostview, Autocompletetextview,button,calendarview, CheckBox, CheckedTextView, Chronometer, Compoundbutton,
the direct subclasses derived from ViewGroup are: Absolutelayout,adapterview,fragmentbreadcrumbs,framelayout, Linearlayout,relativelayout, Slidingdrawer
the indirect subclasses derived from ViewGroup are: Abslistview,absspinner, Adapterviewanimator, Adapterviewflipper, Appwidgethostview, CalendarView, DatePicker, Dialerfilter, Expandablelistview, Gallery, Gestureoverlayview,gridview, Horizontalscrollview, Imageswitcher,listview,
It is noted here that ImageView is a common class for layouts with graphic effects, and Surfaceview is a very important class for game development compared to General view, and Absolutelayout, Framelayout, LinearLayout, relativelayout the direct subclasses of these viewgroup are the most basic layout elements in the Android UI layout.
Custom controls (custom view and ViewGroup)
dv6300-t Program Editing is the use of custom ViewGroup
about Custom ViewGroup knowledge, we can refer to E:\JAVA\Android\ data \ network data \ Application List sliding summary \applist test code, there will be a deeper understanding.
let us introduce the most important methods of view and ViewGroup-
protected void OnDraw (canvas canvas): The method used for redrawing in the view class, which is the method that all view, ViewGroup, and its derived classes have, and is the most important method for the Android UI drawing. Developers can reload the method and draw their own various graphics and image effects within the overloaded method based on the parameter canvas.
protected void OnLayout (Boolean changed, int left, int top, int. right, int bottom): The method that will be called when layout changes in the view class, this method is all view, V Iewgroup and its derived classes have methods that overload the class to be customized when the layout changes, which is useful when implementing some effects.
protected void Dispatchdraw (canvas canvas): ViewGroup class and its derived classes have methods that are primarily used to control the drawing and distribution of sub-view, overloading the method to alter the rendering of child view, In order to achieve some complex visual effects, a typical example can be found in the Dispatchdraw overload of the Launcher module workspace.
protected Boolean drawchild (canvas canvas, View child, Long Drawingtime)): The method that the ViewGroup class and its derived classes have, This method directly controls the drawing of a specific sub-view of a bureau, overloading the method can control a specific sub-view.
The AddView method is used to add components to the View container. We can use this method to add components to this viewgroup.
Getchildat Method This method is used to return the view at the specified location.
Note: The view in ViewGroup is counted starting at 0.
The view appears on the screen to go through measure (calculation) and layout.
onmeasure (int, int) View calls this method to confirm the size of itself and all child objects
onlayout (boolean, int, int, int, int, int, int) Call this method when view wants to allocate size and position for all child objects
onsizechanged (int, int, int, int) Call this method when the view size changes
protected void onmeasure (int widthmeasurespec, int heightmeasurespec) describes:
onmeasure incoming Widthmeasurespec and heightmeasurespec are not general dimensional values, but are values that combine patterns and dimensions.
It is generally based on the definitions in the XML file, and we can know the pattern and size based on these 2 parameters.
we need to get the pattern through int mode = Measurespec.getmode (Widthmeasurespec),
use int size = Measurespec.getsize (Widthmeasurespec) to get the dimensions.
mode A total of three cases, the value is Measurespec.unspecified, measurespec.exactly, Measurespec.at_most.
Correspondence Relationship:
-2147483648----The wrap_content----in the XML file Measurespec.at_most
1073741824----fill_parent-----in XML files measurespec.exactly
0-----measurespec.unspecified
in general ViewGroup, we realize this:
protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {
super.onmeasure (Widthmeasurespec, heightmeasurespec);
Final int width = measurespec.getsize (widthmeasurespec);
final int widthmode = Measurespec.getmode (widthmeasurespec);
Final int count = Getchildcount ();
for (int i = 0; i < count; i++) {
//In fact, for our own application, the best way is to remove the framework of the method, directly call View.measure (), as follows:
//General we set ViewGroup XML layout is wrap_content, so that 2 parameters is 2147483648, then we call the following is
//Komi components to fit their own size
Getchildat (i). Measure (Widthmeasurespec, heightmeasurespec);
//And the following method
//The entire measure () process is a recursive process
//The method is just a filter, and the measure () process is called at the end, or the Measurechild (child, H, i) method
//measurechildwithmargins (Getchildat (i)., h, i);
}
ScrollTo (Mcontrol.getcurscreen () * width, 0);
}
Of course we can also call Setmeasureddimension (H, L) to set the size of the viewgroup.
As for the onmeasure implementation of the view, we generally do not overwrite the method, the word is also simple, according to the need, as above, according to the 2 parameters passed to get the current pattern and size.
Of course we can also calculate the size ourselves, call setmeasureddimension settings.
ViewGroup's OnLayout implementation:
protected void OnLayout (Boolean changed, int l, int t, int r, int b) {
int childleft = 0;
//Get the number of all sub-view
final int childCount = Getchildcount ();
for (int i = 0; i < ChildCount; i++) {
final View Childview = Getchildat (i);
final int childwidth = Childview.getmeasuredwidth ();
final int childheight = Childview.getmeasuredheight ();
childview.layout (childleft, 0, Childleft + childwidth,childheight);
//left side of next view + one
Childleft + = childwidth;
}
}
It's actually very simple to call the layout method to set the view position on the canvas, which can go beyond the screen width, and then we can scroll through the display.
of course, we can also call layout to pass in the relevant coordinates to set the view display location (verified OK)
Test Code:
E:\JAVA\Android\ data \ network data \ Application List Sliding summary \applist
E:\JAVA\Android\ data \ Network data \view and Viewgroup\viewgruop_viewtest

What is the difference between view and ViewGroup?

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.