What is the difference between a View
and a ViewGroup
in Android programming?
问题:
回答1:
View
View
objects are the basic building blocks of User Interface(UI) elements in Android.View
is a simple rectangle box which responds to the user's actions.- Examples are
EditText
,Button
,CheckBox
etc.. View
refers to theandroid.view.View
class, which is the base class of all UI classes.
ViewGroup
ViewGroup
is the invisible container. It holdsView
andViewGroup
- For example,
LinearLayout
is theViewGroup
that contains Button(View), and other Layouts also. ViewGroup
is the base class for Layouts.
回答2:
Below image is the answer. Don't take it too complex.
回答3:
A
ViewGroup
is a special view that can contain other views (called children.) The view group is the base class for layouts and views containers. This class also defines theViewGroup.LayoutParams
class which serves as the base class for layouts parameters.View
class represents the basic building block for user interface components. A View occupies a rectangular area on the screen and is responsible for drawing and event handling. View is the base class for widgets, which are used to create interactive UI components (buttons, text fields, etc.).- Example : ViewGroup (LinearLayout), View (TextView)
Reference
回答4:
View
is a basic building block of UI
(User Interface) in android. A view is a small rectangular box which responds to user inputs. Eg: EditText
, Button
, CheckBox
, etc..
ViewGroup
is a invisible container of other views (child views) and other viewgroups. Eg: LinearLayout
is a viewgroup which can contain other views in it.
ViewGroup
is a special kind of view which is extended from View as its base class. ViewGroup
is the base class for layouts.
as name states View is singular and the group of Views is the ViewGroup
.
more info: http://www.herongyang.com/Android/View-ViewGroup-Layout-and-Widget.html
回答5:
Viewgroup inherits properties of views and does more with other views and viewgroup.
See the Android API: http://developer.android.com/reference/android/view/ViewGroup.html
回答6:
ViewGroup
is itself a View
that works as a container for other views. It extends the functionality of View
class in order to provide efficient ways to layout the child views.
For example, LinearLayout
is a ViewGroup
that lets you define the orientation in which you want child views to be laid, that's all you need to do and LinearLayout
will take care of the rest.
回答7:
in ViewGroup
you can add some other View
s as child. ViewGroup
is the base class for layouts and view containers.
回答8:
View
is the SuperClass of All component like TextView, EditText, ListView,
etc..
while ViewGroup
is Collection of Views(TextView, EditText, ListView, etc..)
, somewhat like container.
回答9:
A ViewGroup describes the layout of the Views in its group. The two basic examples of ViewGroups are LinearLayout and RelativeLayout. Breaking LinearLayout even further, you can have either Vertical LinearLayout or Horizontal LinearLayout. If you choose Vertical LinearLayout, your Views will stack vertically on your screen. The two most basic examples of Views are TextView and Button. Thus, if you have a ViewGroup of Vertical LinearLayout, your Views (e.g. TextViews and Buttons) would line up vertically down your screen.
When the other posters show nested ViewGroups, what they mean is, for example, one of the rows in my Vertical LinearLayout might actually, at the lower level, be several items arranged horizontally. In that case, I'd have a Horizontal LinearLayout as one of the children of my top level Vertical LinearLayout.
Example of Nested ViewGroups:
Parent ViewGroup = Vertical LinearLayout
Row1: TextView1
Row2: Button1
Row3: Image TextView2 Button2 <-- Horizontal Linear nested in Vertical Linear
Row4: TextView3
Row5: Button3