我正在查看 Honeycomb Gallery 示例代码 (here),并在尝试在自己的应用中添加操作项时遇到了以下代码:
<item android:id="@+id/camera"
android:title="Camera"
android:icon="?attr/menuIconCamera"
android:showAsAction="ifRoom" />
?attr
让我陷入循环。有人可以解释这是在做什么吗?这与可绘制对象有什么关系?我似乎无法在 Google 上找到任何好的信息。还有我们可以为图标使用的属性列表或库,而不仅仅是 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>
不幸的是,这让我更加困惑。这是在做什么?
最佳答案
?attr/menuIconCamera
值表示将使用当前主题的 menuIconCamera
属性中的图标。
themes.xml
文件中的某处必须有一个可绘制对象分配给 menuIconCamera
属性。如果有两个主题具有不同的此属性值,则实际图标将取决于当前使用的主题。
attrs.xml
文件用于定义自定义属性。如果没有这个定义,编译器会将未知属性视为错误。
关于android - 有人可以解释一下attr吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7504967/