简体   繁体   中英

view and viewgroup android

I know java basics and I am learning Android development now. I have learnt that there are two central android classes for the ui, View and ViewGroup . I want you to correct me wherever I am wrong.

ViewGroup is a subclass of View and holds View objects together. TextView , EditText , etc are subclasses of View . LinearLayout , Gridview , etc are subclasses of ViewGroup .

I want to relate this to the OOP concept of java but I don't seem to get all this. Where are the objects? How come a subclass is a container?

I'll give you a basic overview here. I'm also pretty new to Android, so the correct-me-if-I'm-wrong thing goes for me too. :)

A View is basically a unit of UI; like, say, a box of 24x24 pixels (yes, THAT basic). Now this box can be used for anything, because it is the top-most entity in the hierarchy. We can define it more precisely by specifying what we want it to hold, this is where TextView, ImageView, WebView, etc. come in. So this box 24x24 may hold text, an image or a web-page content, respectively.

A ViewGroup can belong in the 'etc.' above. Just as for showing text, the 24x24 box can be used to display 4 boxes 12x12. If so, the View can be classified as a ViewGroup.

Further, when we know what type of arrangement we require the box to hold, in this case, we can further classify it as any of LinearLayout, RelativeLayout, GridLayout, FrameLayout, etc.

In such a hierarchy, an entity can have a child object of any kind, even an instance of its own class.

You may even say that View is the ' Object ' of UI.

I hope I have helped you.

Comments/edits welcome. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM