?attr/selectableItemBackground 的定义和确切函数

Definition and exact function of ?attr/selectableItemBackground

我通过 android - apply selectableItemBackground in xml with support v7 偶然发现了表达式 ?attr/selectableItemBackground
我想研究一下该表达式的确切功能,因为我不明白开头的问号代表什么以及它究竟是如何完成任务的。

它说它是支持库 v7 的一部分,但我尝试查找它但找不到对

有用的见解

语法?attr/something表示"use the value of the attribute named {something} that was defined for the current theme"。

selectableItemBackground 是应用主题中的属性名称(通常在 styles.xml 中)。您可能没有在您的主题中为它设置一个值,但它可能在您的主题扩展的父主题中具有一个值,因此您的主题也具有该值。

当您可能在使用不同主题的地方使用相同的布局时,此语法很有用。例如,假设您有两个主题:

<style name="Theme.Foo" parent="..." >
    <item name="android:textColorPrimary">@android:color/white</item>
    ...
</style>

<style name="Theme.Bar" parent="..." >
    <item name="android:textColorPrimary">@android:color/black</item>
    ...
</style>

并假设在您的一个布局文件中有这样的内容:

<TextView
    ...
    android:textColor="?android:attr/textColorPrimary" />

根据展开布局时使用的是这两个主题中的哪一个(例如,当您使用 setContentView() 时),TextView 的文本颜色可以是白色或黑色。