Difference between View and ViewGroup in Android

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

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

11条回答
  •  刺人心
    刺人心 (楼主)
    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

提交回复
热议问题