Android的“?colorPrimary"对比“?attr/colorPrimary"? [英] Android "?colorPrimary" vs "?attr/colorPrimary"?
问题描述
我找不到有关以下两者之间差异的任何信息:
I can't find any information about the differences between:
android:textColor="?attr/colorPrimary"
vs
android:textColor="?colorPrimary"
我已经读到?attr"表示当前主题中指定的属性值,但没有"attr"时,它给出的结果相同(=主题中定义的颜色).它的行为与其他属性相似吗?
I have read that "?attr" means attribute value specified in the current theme, but without "attr" it gives the same result (=color defined in my theme). It behaves similar with other attributes?
例如:
是android:background="?attr/selectableItemBackground"
等于android:background="?selectableItemBackground"
吗?
非常感谢.
推荐答案
好,我终于在文档中找到了
Ok, I have finally found in the docs:
由于系统资源工具知道在这种情况下需要属性资源,因此您无需显式声明类型(类型为android:attr/textColorSecondary),可以排除attr类型.
Because the system resource tool knows that an attribute resource is expected in this context, you do not need to explicitly state the type (which would be ?android:attr/textColorSecondary)—you can exclude the attr type.
因此,我得出这样的结论:AppCompat库属性被视为自定义属性,因此,我们用问号将其指向而没有"android"关键字. "attr"是资源的类型,对于平台属性和自定义属性都是可选的(来自docs). Android文档
So, I deducted it's like this: the AppCompat library attributes are treated as custom attributes and as a result we point them by a question mark without "android" keyword. And "attr" is type of resource which is optional for both platform and custom attributes (from docs). Android documentation
这篇关于Android的“?colorPrimary"对比“?attr/colorPrimary"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!