android - 使用支持v7在xml中应用selectableItemBackground

CQM*_*CQM 70 android android-xml android-support-library

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

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

使我的IDE,Eclipse抛出错误(阻止我编译),通知我selectableItemBackground仅适用于最小Api 11及以上.

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

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

Vik*_*ram 204

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

android:background="?attr/selectableItemBackground"
Run Code Online (Sandbox Code Playgroud)

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

  • @SomeoneSomewhere Android系统提供的任何内容都是通过`android:`命名空间前缀访问的.这不包括支持库,因为它们是附加组件.属性在`attrs.xml`中定义,并在`themes.xml`和/或`styles.xml`中设置.所以,如果你要将自己的drawable分配给`selectableItemBackground`,你就不会使用`android:`命名空间.但是,如果drawable是由Android系统提供的话,你会的. (13认同)
  • 哇,这个信息确实很有用,谢谢! (4认同)
  • 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).关于第二个问题,您是否在询问用户定义的属性如何工作? (2认同)

Sun*_*nny 16

这是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>
Run Code Online (Sandbox Code Playgroud)

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

../platforms/android-14/data
Run Code Online (Sandbox Code Playgroud)