View
和 Android 中的小部件有什么区别?
最佳答案
如 View
中所述类文档:
This 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.).The
ViewGroup
subclass is the base class for layouts, which are invisible containers that hold otherView
s (or otherViewGroup
s) and define their layout properties.
因此,View
是 UI 元素的基类和 Widget
大致定义为 任何可以使用的 View
.
让我们再强调一下这些概念。
View
一个 View
是所有 UI 元素的基类。因此,它涵盖了许多不同的类和概念,包括小部件、ViewGroup
s 和布局。有根View
附加到构成 View
基础的 Window 实例等级制度。一般来说,View
这个词通常用于描述一般的 UI 元素,或引用抽象或基本 UI 类,例如 ViewGroup
秒。
小部件
这个术语有多种定义,但大多数是指“即用型”UI 元素,例如 Button
, ImageView
, EditText
等等。请注意,有些人认为小部件是完整的(非抽象的)而不是容器的 UI 元素(例如 ViewGroup
s (layouts/ListViews
))。还值得注意的是,“widget”是一个包名(android.widget
),文档中提到了以下内容:
The widget package contains (mostly visual) UI elements to use on your Application screen.
因此,将非可视 UI 元素视为小部件以及在小部件包下定义的任何类是合理的。有关小部件包中类的完整列表,请参见此处:http://developer.android.com/reference/android/widget/package-summary.html
应用小部件
不要与 UI 元素小部件混淆,应用小部件是远程的 View
最常显示在用户主屏幕上的层次结构。由文档定义:
App Widgets are miniature application views that can be embedded in other applications (such as the Home screen) and receive periodic updates. These views are referred to as Widgets in the user interface, and you can publish one with an App Widget provider. An application component that is able to hold other App Widgets is called an App Widget host.
ViewGroup
一个 ViewGroup
是 View
的子类并提供 parent 和定位 child 的能力View
s,例如在 Layouts 的情况下。
布局/ View 容器
与 Widgets 一样,没有 Layout 基类,因此它可以松散地定义为任何扩展 ViewGroup
的类。并提供定义 child 位置的能力View
s里面。通常,只有 ViewGroup
带有“布局”一词的子类(如 LinearLayout
, RelativeLayout
)被称为“布局”,其他类扩展 ViewGroup
通常只称为“ View 容器”。
最后,我想建议,每当您提到 View 、小部件或任何其他重要术语时,请明确您的预期定义,以便人们更好地理解您所指的内容。
进一步阅读
关于android - Android中的 View 和小部件有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5168549/