android - 了解android编程的xml文件

标签 android android-studio

我对xml不太有经验,但是,根据w3Schools ,冒号(:)之前的关键工作实际上是创建命名空间的xml方式。例如:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    android:orientation="vertical"
    tools:context="com.hbad.mymessenget.CreateMessageActivity">  

所以,我的问题是,这些变量存储在哪里以及如何存储,以及如何在我们的 Android 程序中访问这些变量?具体以android studio(当前版本3.4)为例

最佳答案

XML is nothing but a structured text file.

我将在其他方面尽可能简单地分解它,以帮助您理解它是如何工作的。

例如:您无法在设备上创建内容为 int abc = 12000;.txt 文件,并期望您的设备专门存储该值将 abc 放入其内存中只是因为它看起来像一个变量。 :/ 根本不需要。对于包含内容的 XML 文件也是如此,内容保留在 xml 文件中,直到需要为止

当 Android UI 启动一个应用程序时,它会检查其他 Activity 的 xml 文件以读取它、解释它,并确定如何向用户绘制合理的显示。

逐行读取文件

如果 Android UI 看到:

<LinearLayout
   android:layout_width="match_parent"
   android:layout_height="match_parent"

它告诉操作系统:创建一个线性部分,该部分将在宽度和高度上填充应用程序屏幕

如果看到这个:

android:background="@color/white"

它告诉操作系统:查找名为 color.xml 的文件,读取内容,返回名为白色的项目的值,然后将此布局背景设置为该颜色。

As you can see the OS is not going to any specific memory allocation to retrieve its values because already it resides in the file (either internal or external).

命名空间在所有场景中的作用是什么?

Android 基本上具有三个预定义的 XML 命名空间:app、android、tools

当 Android UI 看到该行时

xmlns:android

它只是告诉操作系统:是的,这个文件是为我们准备的,我们可以像往常一样继续解释它。因此,在文件中的任何位置,它看到带有 android: 的行,它将获取该行的属性,例如 background,然后在 xmlns:android< 中查找定义 命名空间字典(如果存在)以及应如何处理/处理它。其他命名空间也是如此;即 xmlns:toolsxmlns:app,甚至自定义命名空间 xmlns:myNameSpace

如你所见; 命名空间使Android UI能够将其文件与其他文件区分开来,并且还允许具有不同解释的属性的类似定义,从而使xml内容更加健壮和结构化。

供进一步引用。请参阅Android Layouts Vocabulary , Different XML Files Used in Android , XML Namespace Wiki

关于android - 了解android编程的xml文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57442858/

相关文章:

android - 更改android自定义帐户的用户名和密码

android - 如何更改表并添加 onDelete 级联约束 sqlite

android - 如何强制 Android Studio 使用相对路径?

android - 三星远程测试实验室 (RTL) 可以与 Android Studio 一起使用吗

java - 在以编程方式生成的元素上按父级对齐

Android - 在 Eclipse 中创建新 Activity

android - 项目创建时在IntelliJ中添加 'Min SDK Version'

java - 如何使用 navasmdc/MaterialDesignLibrary?

android - 在 android studio 中添加没有菜单或操作栏的新 Activity ?

android-studio - Android Checkstyle-调试自定义检查