【问题标题】:How does android recognized the different attributes of element in xml?android如何识别xml中元素的不同属性?
【发布时间】:2016-12-12 22:52:24
【问题描述】:

我相信以前使用过android的每个人都熟悉xml命名空间声明xmlns:android = xmlns:android="http://schemas.android.com/apk/res/android。这就是我的问题所在。

如果上面的命名空间声明主要是为了唯一标识,那么android:layout_widthandroid:layout_height 等所有属性究竟来自哪里?

我能理解这一点的唯一方法是,如果 xmlns:android="http://schemas.android.com/apk/res/android 是一个包含预定义属性的架构,那么您可以修改所有这些属性。如果是这种情况,那么此架构存储在哪里?

我查看了上面的链接,看看我的帖子是否可能是重复的帖子,但事实并非如此。另一篇文章基本上只是告诉你xmlns:android="http://schemas.android.com/apk/res/android 做了什么。但是,我已经知道该名称空间声明的功能是什么。我关心的是,当上面的命名空间没有引用任何内容时,android 如何知道每个元素的属性是什么?或者至少我认为上面的命名空间没有被引用。

【问题讨论】:

  • Regarding Android xmlns的可能重复
  • 关于您的编辑,there have been several similar questions。而且一直没有明确的答案,至少我已经找到了。您所问的内容非常清楚地记录在这里。 developer.android.com/guide/topics/resources/…
  • 正如那些答案所说“它是一个 URI,而不是一个 URL”,这意味着,你不能只在浏览器中访问该地址,然后期望返回结果
  • 很抱歉让您感到困惑。也许我的问题不是很清楚,所以让我为您重新澄清一下。每当您在诸如相对布局之类的元素标签内键入诸如 android: 之类的内容时,android 都会为您提供有关该属性可能是什么的建议。诸如 android:background、android:layout_width、android:padding 之类的东西。现在我的问题不是这些属性是什么以及它们做什么,而是这些属性在哪里定义。我确信它们必须在某个地方,以便 android 知道为特定元素将它们拉起来。

标签: android xml


【解决方案1】:

命名空间不引用任何内容。有必要知道要提取哪些属性。

这是ViewGroup 的 Android 源代码的 sn-p。

<declare-styleable name="ViewGroup_Layout">
    <!-- Specifies the basic width of the view.  This is a required attribute
         for any view inside of a containing layout manager.  Its value may
         be a dimension (such as "12dip") for a constant width or one of
         the special constants. -->
    <attr name="layout_width" format="dimension">
        <!-- The view should be as big as its parent (minus padding).
             This constant is deprecated starting from API Level 8 and
             is replaced by {@code match_parent}. -->
        <enum name="fill_parent" value="-1" />
        <!-- The view should be as big as its parent (minus padding).
             Introduced in API Level 8. -->
        <enum name="match_parent" value="-1" />
        <!-- The view should be only big enough to enclose its content (plus padding). -->
        <enum name="wrap_content" value="-2" />
    </attr>
    <!-- Specifies the basic height of the view.  This is a required attribute
         for any view inside of a containing layout manager.  Its value may
         be a dimension (such as "12dip") for a constant height or one of
         the special constants. -->
    <attr name="layout_height" format="dimension">
        <!-- The view should be as big as its parent (minus padding).
             This constant is deprecated starting from API Level 8 and
             is replaced by {@code match_parent}. -->
        <enum name="fill_parent" value="-1" />
        <!-- The view should be as big as its parent (minus padding).
             Introduced in API Level 8. -->
        <enum name="match_parent" value="-1" />
        <!-- The view should be only big enough to enclose its content (plus padding). -->
        <enum name="wrap_content" value="-2" />
    </attr>
</declare-styleable>

https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/values/attrs.xml#3005

android如何知道每个元素有哪些属性

defining custom attributes for your custom View 类没有什么不同,它从位于 SDK 中的 res/values/attrs.xml 中提取。

在那里引用一个部分。

定义自定义属性后,您可以在布局 XML 文件中使用它们,就像内置属性一样。唯一的区别是您的自定义属性属于不同的命名空间。它们不属于http://schemas.android.com/apk/res/android命名空间,而是属于http://schemas.android.com/apk/res/[your package name]

所以,android 只是内置属性的命名空间。

【讨论】:

  • 感谢,这正是我想要的。但只是一个我不明白的快速部分。 res/values/atttrs.xml 似乎不适合“schemas.android.com/apk/res/android”。这对我来说有点奇怪,因为 res/values != res/android 但是你仍然可以引用属性。您介意在这部分详细说明一下吗?
  • your.package.name.R 用于从Java 访问您的资源,然后有android.R 用于访问SDK 资源。这是相同的概念,只是 XML 的工作原理。 res/[your package name] == your.package.name.Rres/android == android.R
  • 哦,好吧 res/android == android.R 但如果是这种情况,我不需要做 res/android/attr 来引用声明样式中的属性吗?
  • 不。继续尝试。恐怕行不通。该命名空间适用于 SDK attrs.xml 文件中的所有资源,该文件与所有其他资源一起捆绑在 android.R 中。 /attr 在我看来只是多余的
猜你喜欢
  • 1970-01-01
  • 2015-12-23
  • 1970-01-01
  • 1970-01-01
  • 2018-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-10
相关资源
最近更新 更多