了解用于安卓编程的 XML 文件



我对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 程序中访问它们?特别是在安卓工作室(当前版本3.4(的情况下

XML只不过是一个结构化的文本文件。

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

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

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

逐行读取文件

如果安卓界面看到:

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

它告诉操作系统:Create a linear section that will fill the app screen both in width and in height

如果它看到以下内容:

android:background="@color/white"

它告诉操作系统:Look for a file named color.xml, read the content, bring back the value of an item named white and then make this layout background that color.

如您所见,操作系统不会进行任何特定的内存分配 检索其值,因为它已驻留在文件(内部或外部(中。

命名空间在所有塞纳里奥的工作是什么?

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

当安卓 UI 看到这条线时

xmlns:android

它只是告诉操作系统:Yes, this file is meant for us and we can go on to interpret it like we always do.因此,在文件中的任何地方,它都会看到一行android:它会得到该行的属性,例如background然后在xmlns:android命名空间字典中查找定义(如果存在(以及应该如何处理/处理它。其他命名空间也是如此;即xmlns:toolsxmlns:app,甚至自定义命名空间xmlns:myNameSpace

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

供进一步参考。请参阅Android布局词汇表,Android中使用的不同XML文件,XML命名空间Wiki

它们不是变量。

xmlns:定义供其他属性使用的 XML 命名空间。

tools:用于针对 Android Studio 和其他开发工具的属性;这些属性不包含在您的应用中。

android:app:适用于对应用具有运行时影响的属性。有关这些属性的详细信息,请参阅文档,例如LinearLayout的文档。有关 Android 应用开发的书籍和课程中也介绍了这些属性的使用。

最新更新