Difference between View and ViewGroup in Android

后端 未结 11 1484
暖寄归人
暖寄归人 2020-12-02 08:11

What is the difference between a View and a ViewGroup in Android programming?

相关标签:
11条回答
  • 2020-12-02 08:41
    1. 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 the ViewGroup.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.).

    2. Example : ViewGroup (LinearLayout), View (TextView)

    Reference

    0 讨论(0)
  • 2020-12-02 08:44

    A View object is a component of the user interface (UI) like a button or a text box, and it's also called widget.

    A ViewGroup object is a layout, that is, a container of other ViewGroup objects (layouts) and View objects (widgets). It's possible to have a layout inside another layout. It's called nested layout but it can increase the time needed to draw the user interface.

    The user interface for an app is built using a hierarchy of ViewGroup and View objects. In Android Studio it is possible to use the Component Tree window to visualise this hierarchy.

    The Layout Editor in Android Studio can be used to drag and drop View objects (widgets) in the layout. It simplifies the creation of a layout.

    0 讨论(0)
  • Below image is the answer. Don't take it too complex.

    0 讨论(0)
  • 2020-12-02 08:45

    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

    0 讨论(0)
  • 2020-12-02 08:47

    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

    0 讨论(0)
  • 2020-12-02 08:47

    in ViewGroup you can add some other Views as child. ViewGroup is the base class for layouts and view containers.

    0 讨论(0)
提交回复
热议问题