Android View and ViewGroup

In Android, a ViewGroup inherited from View . A ViewGroup is a container that contains Views .

 ViewGroup (LinearLayout) View (TextView) 

Why do people on Android define this relationship as Inheritance instead of composition. Since the ViewGroup contains Views , should it not be a composition?

+7
android view viewgroup
Dec 23 '11 at 6:05
source share
6 answers

I think you hanged yourself too much on the wording.

"ViewGroup" has as many reasons as possible to inherit from "View" in the form of "TextView", "ImageView" or ... moreover ... "ScrollView" or "SurfaceView", (the last two "contain things").

Perhaps "Browse" was not necessarily the best choice of terms ... but the class hierarchy makes full sense. Regardless of what are called subclasses :)

IMHO ...

+11
Dec 23 '11 at 6:12
source share

I think this is a great example of a Composite template:

http://en.wikipedia.org/wiki/Composite_pattern

Even if the name cannot be the best ...

+3
May 20 '13 at 13:03
source share

Reading an official document is the golden rule.

ViewGroup is a special view that may contain other views (called children.) A view group is a base class for layouts and views. This class also defines the ViewGroup.LayoutParams class, which serves as the base class for layout options.




If you still donโ€™t know what it is, do a Google image search:

enter image description here

+2
Aug 28 '15 at 10:52
source share

A ViewGroup is a (subclass of) View , as it can serve as an important view:

  • It may be an element in the layout XML file.
  • It can be displayed on the screen (displaying its child views, its own background color, etc.).
  • It builds up along with the rest of the hierarchy of views.
  • It can serve as a representation of the contents of an activity (via setContentView() )

So this is really a View .

I agree that the classname of the ViewGroup bit confusing because it looks like a group, not a view. Calling ViewGroupView can be more logical if it is bulky.

+1
Aug 6 '15 at 19:59
source share

Viewgroup inherits view properties and does more with other views and a view group

0
Dec 23 '11 at 6:53
source share

ViewGroup is a special view that may contain other views. - A view group is a base class for layouts and container views - For example, RelativeLayout is a ViewGroup that contains TextView (View) and other layouts.

link to information: https://developer.android.com/reference/android/view/ViewGroup.html

A View is the basic building block for user interface components โ€” It occupies a rectangle on the screen and is responsible for drawing and processing events. - Examples - EditText, Button, TextView, etc.

link to information: https://developer.android.com/reference/android/view/View.html

0
Mar 05 '18 at 10:27
source share



All Articles