有人能解释一下吗?

我正在看蜂巢画廊的样本代码(给你) ,当我试图在我自己的应用程序中添加行动项目时,我遇到了以下代码:

<item android:id="@+id/camera"
android:title="Camera"
android:icon="?attr/menuIconCamera"
android:showAsAction="ifRoom" />

?attr让我大吃一惊。有人能解释一下这是怎么回事吗?这和绘画有什么关系?我似乎在谷歌上找不到任何有用的信息。还有一个列表或图片库的属性,我们可以使用图标,而不仅仅是 menuIconCamera

谢谢

编辑: 我做了更多的调查,发现 attrs.xml 是这样的:

<resources>
<declare-styleable name="AppTheme">
<attr name="listDragShadowBackground" format="reference" />
<attr name="menuIconCamera" format="reference" />
<attr name="menuIconToggle" format="reference" />
<attr name="menuIconShare" format="reference" />
</declare-styleable>

不幸的是,这只会让我更困惑,这是怎么回事?

45713 次浏览

The ?attr/menuIconCamera value means that an icon from menuIconCamera attribute of the current theme will be used.

There must be a drawable assigned to the menuIconCamera attribute somewhere in the themes.xml file. If there're two themes with different values of this attribute then actual icon will depend on a theme which is currently used.

The attrs.xml file is used to define custom attributes. Without this definition compiler will treat unknown attributes as erroneous.

The ?attr: syntax is used for accessing attributes of current theme. See referencing style attributes.

This is for refering style Attributes. see R.attr

?[<package_name>:][<resource_type>/]<resource_name>

Referencing style attributes

My English is not good, sorry. But I know this question

android:icon="?attr/menuIconCamera" want use

attrs.xml

<resources>
<declare-styleable name="AppTheme">
<attr name="listDragShadowBackground" format="reference" />
<attr name="menuIconCamera" format="reference" />
<attr name="menuIconToggle" format="reference" />
<attr name="menuIconShare" format="reference" />
</declare-styleable>
</resources>

styles.xml

<style name="AppTheme.Light" parent="@android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">@style/ActionBar.Light</item>
<item name="android:windowActionBarOverlay">true</item>
<item name="listDragShadowBackground">@android:color/background_light</item>
<item name="menuIconCamera">@drawable/ic_menu_camera_holo_light</item> //this....
<item name="menuIconToggle">@drawable/ic_menu_toggle_holo_light</item>
<item name="menuIconShare">@drawable/ic_menu_share_holo_light</item>
</style>

use @drawable/ic_menu_camera_holo_light

I know this post is very old, but I feel the following explanation will help beginners understand it easily.

So in layman's terms,

someAttribute="?attr/attributeName" means -

set the value of someAttribute to whatever is the value of attributeName in current theme

A common example occurs in styling a Toolbar

<style name="AppTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/primary_color</item>
//some more stuff here
</style>
<!-- custom toolbar style -->
<style name="myToolbar" parent="Widget.AppCompat.Toolbar">
<item name="android:background">?attr/colorPrimary</item>
//some code here
</style>

Here value of android:background will be set to @color/primary_color because ?attr/colorPrimary refers to @color/primary_color in the current theme (AppTheme)

This blog post does an amazing job going over how to reference values for style-attributes that are defined in the current theme: https://trickyandroid.com/android-resources-and-style-attributes-cheatsheet/

  • When you see ? notation - it means that we are trying to reference a style attribute - a value which may vary depending on current theme. In each specific theme we can override this attribute, so the XML layout doesn't need to be changed, and the correct theme needs to be applied.

  • When you see the @ notation - we reference actual resource value (color, string, dimension, etc). This resource should have an actual value. In this case we know exactly what value we are dealing with.

Here's an example:

    <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>


<style name="LauncherButton" parent="TextAppearance.AppCompat.Medium">
<item name="android:textColor">?colorAccent</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_centerHorizontal">true</item>
<item name="android:textAllCaps">false</item>
</style>