?android:attr/colorPrimary 对比 ?attr/colorPrimary

?android:attr/colorPrimary vs ?attr/colorPrimary

有人能告诉我

之间有什么区别吗
?android:attr/colorPrimary

?attr/colorPrimary

无论我用哪个,结果都是一样的。尽管第一个选项在某些设备上会导致 android.view.InflateException

两者的工作原理几乎相同。当您使用 ?attr/colorPrimary 时,它的工作完全正常,因为编译器已经知道必须附加 'android'。

关于你说 ?android:attr/colorPrimary 给你例外,那么在这种情况下,请尝试仅使用第二个选项..

例如在您的 styles.xml 中:以下 may/may 并非每次都有效

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:colorPrimary">@color/primary_material_dark</item>
        <item name="android:colorPrimaryDark">@color/primary_dark_material_dark</item>
    </style>
</resources>

但这主要有效:

<style name="AppTheme"  parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/primary_material_dark</item>
    <item name="colorPrimaryDark">@color/primary_dark_material_dark</item>
</style>

建造者知道颜色来自 attr 所以它是多余的。文档指出:

... you do not need to explicitly state the type ... you can exclude the attr type.

参见:App resources overview: Referencing style attributes

至于 android: - android:colorPrimary 中的前缀指的是 material 主题(API 21 及更高版本)的属性。如果没有前缀,?colorPrimary 将从支持库中获取。