简体   繁体   English

Android 视图和视图组

[英]Android View and ViewGroup

In Android ViewGroup inherits from View .在 Android 中ViewGroup继承自View A ViewGroup is a container which holds Views . ViewGroup是一个包含Views的容器。

ViewGroup (LinearLayout)
View (TextView)

Why did folks at Android defined this relationship as Inheritance instead of composition.为什么 Android 的人们将这种关系定义为Inheritance而不是组合。 As the ViewGroup contains Views shouldn't it be composition ?由于ViewGroup包含Views不应该是组合吗?

I think you're getting too hung up on the wording.我认为你对措辞太执着了。

A "ViewGroup" has every bit as much reason to inherit from a "View" as a "TextView", and "ImageView" or ... more to the point ... a "ScrollView" or a "SurfaceView" (the latter two both "contain things"). “ViewGroup”完全有理由从“View”继承“TextView”和“ImageView”或……更重要的是……“ScrollView”或“SurfaceView”(后者两个都“包含东西”)。

Perhaps "View" wasn't necessarily the best choice of terms ... but the class heirarchy makes complete sense.也许“视图”不一定是术语的最佳选择……但类层次结构完全合理。 Regardless of what it's subclasses are named :)不管它的子类被命名为什么:)

IMHO ...恕我直言 ...

I think this is a great example of the Composite design pattern:我认为这是复合设计模式的一个很好的例子:

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

Even though the naming might not be the best...即使命名可能不是最好的......

A ViewGroup is a (subclass of) View because it can serve as a view in important ways: ViewGroupView的(子类),因为它可以在重要方面充当视图:

  • It can be an element in a layout XML file它可以是布局 XML 文件中的一个元素
  • It can be displayed on the screen (by displaying its child views, its own background color, etc.)它可以显示在屏幕上(通过显示它的子视图,它自己的背景颜色等)
  • It gets inflated along with the rest of the view hierarchy它与视图层次结构的其余部分一起膨胀
  • It can serve as an activity's content view (via setContentView() )它可以作为活动的内容视图(通过setContentView()

So it really is a View .所以它真的是一个View

I agree that the classname ViewGroup is a bit confusing, because it sounds like it's a group, not a view.我同意类名ViewGroup有点混乱,因为它听起来像是一个组,而不是一个视图。 Calling it ViewGroupView might have been more logical, if unwieldy.如果笨拙,将其称为ViewGroupView可能更合乎逻辑。

Why did folks at Android define this relationship as Inheritance instead of composition?为什么 Android 的人们将这种关系定义为继承而不是组合? As the ViewGroup contains Views shouldn't it be composition?由于 ViewGroup 包含 Views 不应该是组合吗?

In a case like this, inheritance and composition are not mutually exclusive.在这种情况下,继承和组合并不相互排斥。 A ViewGroup is a View (inheritance) and a ViewGroup can contain Views (composition). ViewGroup 是一个视图(继承) 一个 ViewGroup 可以包含视图(组合)。

Reading the official doc is the golden rule.阅读官方文档是黄金法则。

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. ViewGroup 是一个特殊的视图,可以包含其他视图(称为子视图)。视图组是布局和视图容器的基类。 This class also defines the ViewGroup.LayoutParams class which serves as the base class for layouts parameters.该类还定义了 ViewGroup.LayoutParams 类,该类用作布局参数的基类。


If you still do not find out what it is, search with Google image:如果您仍然没有找到它是什么,请使用谷歌图片搜索:

在此处输入图片说明

A ViewGroup is a special view that can contain other views. ViewGroup 是一种特殊的视图,可以包含其他视图。 - The view group is the base class for layouts and views containers - For example, RelativeLayout is the ViewGroup that contains TextView(View), and other Layouts also. - 视图组是布局和视图容器的基类 - 例如,RelativeLayout 是包含 TextView(View) 和其他布局的 ViewGroup。

refer link for info: https://developer.android.com/reference/android/view/ViewGroup.html参考信息链接: https : //developer.android.com/reference/android/view/ViewGroup.html

A View represents the basic building block for user interface components -It occupies rectangle on the screen and is responsible for drawing and handling events.视图代表用户界面组件的基本构建块——它在屏幕上占据矩形,负责绘制和处理事件。 - Examples are EditText, Button, TextView etc - 示例有 EditText、Button、TextView 等

refer link for info: https://developer.android.com/reference/android/view/View.html参考信息链接: https : //developer.android.com/reference/android/view/View.html

All UI elements in an Android app are built using View and ViewGroup objects. Android 应用程序中的所有 UI 元素都是使用ViewViewGroup对象构建的。

  1. View :- A View is an object that draws something on the screen that the user can interact with. View :- View 是一个对象,它在屏幕上绘制一些用户可以与之交互的东西。
  2. ViewGroup :- ViewGroup is used to hold Views and ViewGroups. ViewGroup :- ViewGroup 用于保存视图和视图组。

视图组继承视图的属性,并与其他视图和视图组做更多的事情

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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