三亩地 三亩地SAN MU DI · CODE DIARY
ARTICLE DETAIL

日记详情

真实记录编程学习的某一天,欢迎挑你感兴趣的翻一翻。

Andriod APP Kotlin 开发学习 001 ------ XML 脚本基础

Andriod APP Kotlin 开发学习 001 ------ XML 脚本基础

Andriod APP 开发学习(Kotlin)

  • 引言
  • 正文

引言

我们将正式开启 Android APP 开发系列教程。今天我们先来搞懂创建完成 new project 后自带 activity_main.xml 脚本的含义。

正文

创建完成后会得到如下脚本:

<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayoutxmlns: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:id="@+id/main"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Hello World!"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"/></androidx.constraintlayout.widget.ConstraintLayout>

<?xml version="1.0" encoding="utf-8"?>: 表示当前脚本文件为 xml 语言,且满足 1.0 规范,且使用的是 utf-8 编码形式。默认开头记住就好,不用更改。且需要注意,对于 xml 脚本,该语句必须放在首行,前面不能有任何内容,否则编译器报错。
<androidx.constraintlayout.widget.ConstraintLayout>: 在 xml 中,<> 中间的部分是一个完整的标签,注释信息不能够放在标签内部。ConstraintLayout 是整个布局的根容器,所有其他控件都要放在里面。可以视作固定写法。
因此,<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:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity">是一个长标签。

xml 中的注释信息无单行多行之分,只需要将注释信息放在<!-- 注释信息 -->内即可。

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":xmlns 是 xml NameSpace 的缩写,可以理解为前缀声明。这三行是在告诉系统:后面出现的 android:、app:、tools: 开头的属性,分别去哪里查定义。需要注意的是,http://schemas.android.com/apk/res/android看起来像是一个网站,但是如果我们尝试打开这个地址是无法打开的,事实上,这个域名是被谷歌持有的,写成这样只是为了区别命名空间的位置与别人的不一样,实际编译器搜索的时候,会去 SDK 中搜索 android,会去项目中或第三方库中搜索 app 和以及在设计预览时才会生效的 tools 开头的属性,打包为 APK 后会自动删除。

android:id="@+id/main": 给当前 ConstraintLayout 这个容器,给一个 id,叫做 main,后续可以通过findViewById(R.id.main)语句找到这个容器。相当于给这个空间贴了一个标签,方便查询。

android:layout_width="match_parent",android:layout_height="match_parent":match_parent:表示宽度和高度填满父容器(也就是整个屏幕)。相当于告诉容器:“我们要占满整个屏幕”。

tools:context=".MainActivity":这行只在 Android Studio 的设计预览中生效,运行时会被忽略。目的是告诉编辑器:“当前这个布局是和 MainActivity 页面关联的”,方便在预览时显示正确的主题和可操作组件。

<TextView/>: 显示文字的控件。wrap_content:表示宽高刚好包裹住文字内容,文字有多大,控件就多大。
android:text="Hello World!":设置要显示的文字内容。

下面的四行代码是 ConstraintLayout 的核心,决定了 TextView 的位置。
app:layout_constraintBottom_toBottomOf="parent":底部 → 对齐到父容器的底部
app:layout_constraintEnd_toEndOf="parent":右边缘 → 对齐到父容器的右边缘
app:layout_constraintStart_toStartOf="parent":左边缘 → 对齐到父容器的左边缘
app:layout_constraintTop_toTopOf="parent":顶部 → 对齐到父容器的顶部
四边都对齐父容器,结果就是:TextView 被锁定在屏幕正中央。

</androidx.constraintlayout.widget.ConstraintLayout>:闭合根容器标签,表示布局结束。

在本文中出现了两种标签,<androidx.constraintlayout.widget.ConstraintLayout ></androidx.constraintlayout.widget.ConstraintLayout>叫做闭合标签,由<androidx.constraintlayout.widget.ConstraintLayout >开始标签和</androidx.constraintlayout.widget.ConstraintLayout>标签组成,它的内部可以插入各类其他控件。而<TextView />叫做自闭合标签,虽然它也可以写为闭合标签两个组成的形式,但是因为它的内部本质上并不需要包含其他组件,因此写成闭合标签的形式会显得冗余。

← 返回列表