关于Android的XML属性的问号(?)

时间:2021-07-15 01:00:53

Can anyone explain the question mark means in Android XML attributes?

有人能解释Android XML属性中问号的含义吗?

<TextView    
    style="?android:attr/windowTitleStyle"
    More attributes
/>

2 个解决方案

#1


111  

The question mark means it's a reference to a resource value in the currently applied theme. See the linuxtopia Android Dev Guide or the android.com Dev Guide for more about it.

问号表示它是当前应用主题中的资源值的引用。请参阅linuxtopia Android Dev Guide或android.com开发指南以了解更多信息。

\? escapes the question mark.

\ ?逃的问号。

#2


25  

The ? lets you refer to a style attribute instead 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 themes containing 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的属性。这个电影图标属性指向一个不同的@drawable资源,比如robot。png或者霍比特人。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“在任何地方的主题都是有效的(比如在工具栏或对话框或任何类型的视图布局),当你切换主题时,它会自动指向正确的drawable。您不需要任何主题相关的逻辑来使用不同的drawables。您只需为每个主题定义movieIcon属性,而Android框架则负责其余部分。

#1


111  

The question mark means it's a reference to a resource value in the currently applied theme. See the linuxtopia Android Dev Guide or the android.com Dev Guide for more about it.

问号表示它是当前应用主题中的资源值的引用。请参阅linuxtopia Android Dev Guide或android.com开发指南以了解更多信息。

\? escapes the question mark.

\ ?逃的问号。

#2


25  

The ? lets you refer to a style attribute instead 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 themes containing 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的属性。这个电影图标属性指向一个不同的@drawable资源,比如robot。png或者霍比特人。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“在任何地方的主题都是有效的(比如在工具栏或对话框或任何类型的视图布局),当你切换主题时,它会自动指向正确的drawable。您不需要任何主题相关的逻辑来使用不同的drawables。您只需为每个主题定义movieIcon属性,而Android框架则负责其余部分。