2013-10-31 22 views
58

即使有支持Android V7包括在我的应用程序机器人 - 应用selectableItemBackground在XML的支持V7

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

让我的IDE,Eclipse将抛出一个错误(阻止我编译),通知我说, selectableItemBackground仅适用于最小的Api 11及以上。

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

假定从一个更高的库,复制和粘贴是不是一个解决方案

回答

165

由于属性是在库(支持V7)定义,你可以使用它作为一个用户定义的属性:即没有android:前缀:

android:background="?attr/selectableItemBackground" 

您所看到的错误指出?android:attr/selectableItemBackground适用于API版本大于等于11的情况。确实如此。

+2

1)您如何知道该属性支持v7,而不是v4? (我现在正在下载支持库的rev19)2)你有没有使用用户定义属性的URI的例子? –

+2

@SomeoneSomewhere从我所知道的,v4没有定义任何属性。我知道'selectableItemBackground'是在v7中通过查看[android.support.v7.appcompat.R.attr]来定义的(http://developer.android.com/reference/android/support/v7/appcompat/R.attr的.html)。关于你的第二个问题,你问的是用户定义的属性是如何工作的? – Vikram

+0

我实际上已经忘记了当我提出问题2时我在做什么。我相信下面的链接回答了这个问题,尤其是评论:http://stackoverflow.com/a/11388952/550471 –

14

这里是selectedItemBackground。您可以在/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目录

../platforms/android-14/data 
+0

所以你建议我将它复制到一个主题或我自己的drawable中,而不是想知道如何从应该使用我的旧api级别的支持库中获取它? – CQM

+0

你可以设置大于11的最小api,否则你必须在'res'文件夹中从那里复制drawables。 – Sunny

+0

我觉得我应该可以设置一个名称空间或其他东西来获取支持库的属性 – CQM

2

不上这方面的专家发现可绘制但似乎你需要基于主题的平台版本。 official guide解释了这个过程我认为很好。

您必须为每个版本创建不同的XML文件,并将它们保存在res/values-v7res/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" /> 

希望这有助于。 干杯。

+0

好吧这个元素“?android:attr/selectableItemBackground”已经在android support v7库中。我不需要复制其他库中的主题或制作特定于平台的代码 – CQM

+2

实际上,kraxor为定制不同API级别的背景提供了一个很好的观点。例如API 21具有21之前不支持的连锁效应(即使使用AppCompat),所以您可能需要以不同方式设置项目背景 –

+2

这是一个很好的解决方案,因为它可以在其他视图上重用。然而,根据我的经验,样式定义中的属性应该是'android:background'而不是'selectableItemBackground',如下所示: '' – kip2