77

即使我的应用程序中包含 android support v7

添加 android:background="?android:attr/selectableItemBackground"

使我的 IDE,Eclipse 抛出一个错误(阻止我编译),通知我 selectableItemBackground 仅适用于 min Api 11 及更高版本。

如何将此属性添加到 XML 中的背景?

假设从更高的库复制和粘贴不是解决方案

4

3 回答 3

220

由于该属性是在库中定义的(支持 v7),您可以将其用作用户定义的属性:即不带android:前缀:

android:background="?attr/selectableItemBackground"

您看到的错误指出?android:attr/selectableItemBackgroundAPI 版本 >= 11 可用。确实如此。

于 2013-11-05T02:03:12.793 回答
16

这是选定的项目背景。您可以在 /platforms/android-14/data/res/themes.xml 中找到它

<selector xmlns:android="http://schemas.android.com/apk/res/android"
          android:exitFadeDuration="@android:integer/config_mediumAnimTime">

    <item android:state_window_focused="false" android:drawable="@color/transparent" />

    <!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
    <item android:state_focused="true"  android:state_enabled="false" android:state_pressed="true" android:drawable="@drawable/list_selector_background_disabled" />
    <item android:state_focused="true"  android:state_enabled="false"                              android:drawable="@drawable/list_selector_background_disabled" />
    <item android:state_focused="true"                                android:state_pressed="true" android:drawable="@drawable/list_selector_background_transition" />
    <item android:state_focused="false"                               android:state_pressed="true" android:drawable="@drawable/list_selector_background_transition" />
    <item android:state_focused="true"                                                             android:drawable="@drawable/list_selector_background_focused" />
    <item android:drawable="@color/transparent" />

</selector>

你可以在你的 Android SDK 目录中找到 drawables

../platforms/android-14/data
于 2013-10-31T19:22:01.073 回答
4

不是该主题的专家,但您似乎需要基于平台版本的主题。我认为官方指南很好地解释了这个过程。

您必须为每个版本创建不同的 XML 文件并将它们保存在res/values-v7等中res/values-v11。然后将这些样式用于您的视图。像这样的东西:

res/values-v7

<style name="LightThemeSelector" parent="android:Theme.Light">
    ...
</style>

res/values-v11

<style name="LightThemeSelector" parent="android:Theme.Holo.Light">
    <item name="selectableItemBackground">?android:attr/selectableItemBackground</item>
    ...
</style>

然后对视图使用样式:

<TextView
    style="@style/LightThemeSelector"
    android:text="@string/hello" />

希望这可以帮助。干杯。

于 2013-10-31T19:37:51.673 回答