视图 (View) 是一个容器,专门负责布局。表现为显示在屏幕上的各种视图,如 TextView、LinearLayout 等。
View 主要分为两类,具体如下表格所示:
| 类别 | 示例 | 特点 |
| — | — | — |
| 单一视图 | 即一个 View,如 TextView、EditText | 不包含子View |
| 视图组 | 即多个 View 组成的 ViewGroup,如 RelativeLayout | 包含子View |
-
View 类是 Android 中各种组件的基类;
-
View 的构造函数有四个,具体如下所示:
public View(Context context) {
}
public View(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public View(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, 0);
}
public View(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
}
- 源码中 View 的构造函数
通过源码的注释我们可以看出:
-
如果 View 是在 Java 代码里面 new 的,则调用第一个构造函数–>View(Context);
-
如果 View 是在 xml 里声明的,则调用第二个构造函数–>View(Context, AttributeSet)。
===============