android - Android中的 View 和小部件有什么区别?

标签 android

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 other Views (or other ViewGroups) 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

一个 ViewGroupView 的子类并提供 parent 和定位 child 的能力View s,例如在 Layouts 的情况下。

布局/ View 容器

与 Widgets 一样,没有 Layout 基类,因此它可以松散地定义为任何扩展 ViewGroup 的类。并提供定义 child 位置的能力View s里面。通常,只有 ViewGroup带有“布局”一词的子类(如 LinearLayoutRelativeLayout )被称为“布局”,其他类扩展 ViewGroup通常只称为“ View 容器”。

最后,我想建议,每当您提到 View 、小部件或任何其他重要术语时,请明确您的预期定义,以便人们更好地理解您所指的内容。


进一步阅读

关于android - Android中的 View 和小部件有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5168549/

相关文章:

android - 旋转圆形物体

java - 使用正确的Locale和24h从API级别15从Millis获取字符串日期

java - 通知管理器

java - 如何在 Android 中处理长按

android - 绑定(bind)Android服务

android - 这个类图正确吗?它是什么意思

android - 如何以编程方式获取前台 Android Activity (在我自己的应用程序中)的快照?

java - Flutter:无法解析配置 ':path_provider:classpath' 的所有 Artifact

java - 读取 JSON 响应

java - 尝试在 android 中通过 JSONarray 验证密码