Android 的 XML 属性中的问号 (?)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2733907/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Question mark (?) in XML attributes for Android
提问by Casebash
Can anyone explain the question mark means in Android XML attributes?
谁能解释一下 Android XML 属性中的问号意味着什么?
<TextView
style="?android:attr/windowTitleStyle"
More attributes
/>
采纳答案by jball
The question mark means it's a reference to a resource value in the currently applied theme. See the linuxtopia Android Dev Guideor the android.com Dev Guidefor more about it.
问号表示它是对当前应用主题中资源值的引用。请参阅linuxtopia Android Dev Guide或android.com Dev Guide了解更多信息。
\?
escapes the question mark.
\?
逃脱问号。
回答by Devon Biere
The ? lets you refer to a style attributeinstead of a specific hard-coded resource. See "Referencing style attributes"in the Android Dev Guide for details.
这 ?允许您引用样式属性而不是特定的硬编码资源。有关详细信息,请参阅Android 开发指南中的“引用样式属性”。
So, how is this actually useful? It makes the most sense when considering multiple themescontaining the same custom resource attribute.
那么,这实际上有什么用呢?在考虑包含相同自定义资源属性的多个主题时,这是最有意义的。
Say you have movie-related themes like MyThemeTransformers and MyThemeHobbit, and both have an attribute called movieIcon. And that movieIcon attribute points to a different @drawable resource, say robot.png or hobbit.png, in each theme definition.
假设您有与电影相关的主题,例如 MyThemeTransformers 和 MyThemeHobbit,并且两者都有一个名为movieIcon的属性。在每个主题定义中,movieIcon 属性指向不同的@drawable 资源,比如robot.png 或hobbit.png。
You can refer to "?attr/movieIcon" anywhere the theme is in effect (like in a toolbar or dialog or whatever kind of View layout), and it will automatically point to the correct drawable when you switch between themes. You don't need any theme-dependent logic to use the different drawables. You just define the movieIcon attribute for each theme and the Android framework takes care of the rest.
您可以在主题有效的任何地方(例如在工具栏或对话框或任何类型的视图布局中)引用“ ?attr/movieIcon”,当您在主题之间切换时,它会自动指向正确的可绘制对象。您不需要任何与主题相关的逻辑来使用不同的可绘制对象。您只需为每个主题定义 movieIcon 属性,其余的由 Android 框架处理。