行业资讯

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

发布时间:2026/8/16 8:48:52
Andriod APP Kotlin 开发学习 001 ------ XML 脚本基础 Andriod APP 开发学习Kotlin引言正文引言我们将正式开启 Android APP 开发系列教程。今天我们先来搞懂创建完成 new project 后自带 activity_main.xml 脚本的含义。正文创建完成后会得到如下脚本?xml version1.0 encodingutf-8?androidx.constraintlayout.widget.ConstraintLayoutxmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:apphttp://schemas.android.com/apk/res-autoxmlns:toolshttp://schemas.android.com/toolsandroid:idid/mainandroid:layout_widthmatch_parentandroid:layout_heightmatch_parenttools:context.MainActivityTextViewandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:textHello World!app:layout_constraintBottom_toBottomOfparentapp:layout_constraintEnd_toEndOfparentapp:layout_constraintStart_toStartOfparentapp:layout_constraintTop_toTopOfparent//androidx.constraintlayout.widget.ConstraintLayout?xml version1.0 encodingutf-8? 表示当前脚本文件为 xml 语言且满足 1.0 规范且使用的是 utf-8 编码形式。默认开头记住就好不用更改。且需要注意对于 xml 脚本该语句必须放在首行前面不能有任何内容否则编译器报错。androidx.constraintlayout.widget.ConstraintLayout 在 xml 中 中间的部分是一个完整的标签注释信息不能够放在标签内部。ConstraintLayout 是整个布局的根容器所有其他控件都要放在里面。可以视作固定写法。因此androidx.constraintlayout.widget.ConstraintLayout xmlns:androidhttp://schemas.android.com/apk/res/android xmlns:apphttp://schemas.android.com/apk/res-auto xmlns:toolshttp://schemas.android.com/tools android:idid/main android:layout_widthmatch_parent android:layout_heightmatch_parent tools:context.MainActivity是一个长标签。xml 中的注释信息无单行多行之分只需要将注释信息放在!-- 注释信息 --内即可。xmlns:androidhttp://schemas.android.com/apk/res/android xmlns:apphttp://schemas.android.com/apk/res-auto xmlns:toolshttp://schemas.android.com/toolsxmlns 是 xml NameSpace 的缩写可以理解为前缀声明。这三行是在告诉系统后面出现的 android:、app:、tools: 开头的属性分别去哪里查定义。需要注意的是http://schemas.android.com/apk/res/android看起来像是一个网站但是如果我们尝试打开这个地址是无法打开的事实上这个域名是被谷歌持有的写成这样只是为了区别命名空间的位置与别人的不一样实际编译器搜索的时候会去 SDK 中搜索 android会去项目中或第三方库中搜索 app 和以及在设计预览时才会生效的 tools 开头的属性打包为 APK 后会自动删除。android:idid/main: 给当前 ConstraintLayout 这个容器给一个 id叫做 main后续可以通过findViewById(R.id.main)语句找到这个容器。相当于给这个空间贴了一个标签方便查询。android:layout_widthmatch_parentandroid:layout_heightmatch_parentmatch_parent表示宽度和高度填满父容器也就是整个屏幕。相当于告诉容器“我们要占满整个屏幕”。tools:context.MainActivity这行只在 Android Studio 的设计预览中生效运行时会被忽略。目的是告诉编辑器“当前这个布局是和 MainActivity 页面关联的”方便在预览时显示正确的主题和可操作组件。TextView/ 显示文字的控件。wrap_content表示宽高刚好包裹住文字内容文字有多大控件就多大。android:textHello World!设置要显示的文字内容。下面的四行代码是 ConstraintLayout 的核心决定了 TextView 的位置。app:layout_constraintBottom_toBottomOfparent底部 → 对齐到父容器的底部app:layout_constraintEnd_toEndOfparent右边缘 → 对齐到父容器的右边缘app:layout_constraintStart_toStartOfparent左边缘 → 对齐到父容器的左边缘app:layout_constraintTop_toTopOfparent顶部 → 对齐到父容器的顶部四边都对齐父容器结果就是TextView 被锁定在屏幕正中央。/androidx.constraintlayout.widget.ConstraintLayout闭合根容器标签表示布局结束。在本文中出现了两种标签androidx.constraintlayout.widget.ConstraintLayout /androidx.constraintlayout.widget.ConstraintLayout叫做闭合标签由androidx.constraintlayout.widget.ConstraintLayout 开始标签和/androidx.constraintlayout.widget.ConstraintLayout标签组成它的内部可以插入各类其他控件。而TextView /叫做自闭合标签虽然它也可以写为闭合标签两个组成的形式但是因为它的内部本质上并不需要包含其他组件因此写成闭合标签的形式会显得冗余。