01Android UI
1.1UI
- 用户界面(User Interface,简称 UI,亦称使用者界面)是系统和用户之间进行交互和信息交换
的媒介,它实现信息的内部形式与人类可以接受形式之间的转换。 - 软件设计可分为两个部分:编码设计与UI设计。
1.2 Android UI
- Android应用界面包含用户可查看并与之交互的所有内容。Android 提供丰富多样的预置 UI 组
件,例如结构化布局对象和 UI 控件,您可以利用这些组件为您的应用构建图形界面。Android 还
提供其他界面模块,用于构建特殊界面,例如对话框、通知和菜单。 - Android UI 都是由布局和控件组成的
02 布局
布局(layout)可定义应用中的界面结构(例如 Activity 的界面结构)。布局中的所有元素均使用 View
和 ViewGroup 对象的层次结构进行构建。View 通常绘制用户可查看并进行交互的内容。然而,
ViewGroup 是不可见容器,用于定义 View 和其他 ViewGroup 对象的布局结构。
布局的结构
- 定义界面布局的视图层次结构图示
View
对象通常称为“微件”,可以是众多子类之一,例如Button
或TextView
。ViewGroup
对象通常称为“布局”,可以是提供其他布局结构的众多类型之一,例如
LinearLayout
或ConstraintLayout
。
2.2 声明布局
-
在 XML 中声明界面元素,Android 提供对应 View 类及其子类的简明 XML 词汇,如用于微件和布
局的词汇。您也可使用 Android Studio 的 Layout Editor,并采用拖放界面来构建 XML 布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</