android - 有人可以解释一下attr吗?

标签 android android-layout attributes android-theme

我正在查看 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/

相关文章:

android - 如何强制android :shape ="oval" to remain perfect circle?

.net - 如何读取程序集属性

android - RecyclerView(水平)嵌套在 BottomSheet 中防止垂直滚动

android - 在自定义 View ListView 项中缩放位图图像

java - 选择地点或尝试返回时,Android 地点选择器显示空白屏幕

android - 使用 AppCompat 时如何更改 ActionBar 标题字体

python - 如何识别属性的属性何时被设置?

attributes - 如何检查边缘是否在 Networkx 中具有属性

android - android中的选择器标签

android - 通过广播 Intent 传递 ArrayList