"android:"前缀在Android framework-res模块中是什么意思?

7

I copy the this code from the styles.xml file in framework-res module

<style name="Theme">

    <item name="colorForeground">@android:color/bright_foreground_dark</item>
    <item name="colorForegroundInverse">@android:color/bright_foreground_dark_inverse</item>
    <item name="colorBackground">@android:color/background_dark</item>

.

<style name="Theme.Black">
    <item name="android:windowBackground">@android:color/black</item>
    <item name="android:colorBackground">@android:color/black</item>
</style>

As you see, they all have a attribute name which's value is windowBackground. But the formar has a android: and the latter doesn't. Is it really necessary to write a android: prefix in android framework?

2个回答

8

我发现这是一个有趣的问题,并尝试探索寻找答案...这就是我找到的...

来源:http://developer.android.com/guide/topics/resources/style-resource.html

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style
        name="style_name"
        parent="@[package:]style/style_to_inherit">
        <item
            name="[package:]style_property_name"
            >style_value</item>
    </style>
</resources>

item - 定义一个样式属性。必须是元素的子级。 属性: name 必需的资源属性。定义要定义的样式属性的名称,必要时带有包前缀(例如android:textColor)。

来源:http://developer.android.com/guide/topics/manifest/manifest-intro.html

资源值 某些属性具有可以显示给用户的值-例如活动的标签和图标。这些属性的值应该本地化,并因此从资源或主题中设置。资源值以以下格式表示,

@[package:]type:name

如果资源与应用程序在同一个包中,则可以省略包名;类型是资源的类型,如“string”或“drawable”,名称是标识特定资源的名称。例如:

主题中的值以类似的方式表示,但是以“?”而不是“@”开头:

?[package:]type:name

最后,我尝试了不使用android:前缀来设置属性,虽然编译成功,但是它抛出了一个异常。


我已经搜索了很多关于Android样式的信息。但这是我得到的最好的答案。我认为它抛出异常可能是由于您编译的环境不在AOSP中。如果给定一个正确的android.mk并在AOSP环境中构建,它可以正常工作。我发现在framework-res模块的android.mk中,有一条语句指定不使用aapt进行编译。 - Frank Cheng
@[package:]type:name?[package:]type:name 应该使用斜杠来分隔类型和名称,就像这样:@[package:]type/name?[package:]type/name,而不是冒号。 - Lv99Zubat

2

访问平台资源

Android包含许多标准资源,例如样式、主题和布局。要访问这些资源,请在资源引用中加上android包名的限定符。例如,Android提供了一个布局资源,您可以在ListAdapter中用于列表项:

setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myarray));

在这个例子中,simple_list_item_1是一个由平台定义的用于在ListView中显示项目的布局资源。您可以使用它来代替创建自己的列表项布局。(想要了解更多关于使用ListView的内容,请参阅List View教程。)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接