【问题标题】:android - apply selectableItemBackground in xml with support v7android - 在支持 v7 的 xml 中应用 selectableItemBackground
【发布时间】:2023-03-13 02:39:01
【问题描述】:

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

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

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

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

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

【问题讨论】:

    标签: android android-xml android-support-library


    【解决方案1】:

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

    android:background="?attr/selectableItemBackground"
    

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

    【讨论】:

    • 1) 你怎么知道该属性在支持 v7 中而不在 v4 中? (我现在正在下载支持库的 rev19)2)你有使用用户定义属性的 URI 示例吗?
    • @SomeoneSomewhere 据我所知,v4 没有定义任何属性。我知道selectableItemBackground 是通过查看android.support.v7.appcompat.R.attr 在v7 中定义的。关于您的第二个问题,您是在问用户定义的属性是如何工作的吗?
    • 当我提出问题 #2 时,我实际上已经忘记了自己在做什么。我相信以下链接回答了这个问题,尤其是评论:stackoverflow.com/a/11388952/550471
    • @SomeoneSomewhere Android 系统提供的任何东西都可以通过android: 命名空间前缀访问。这不包括支持库,因为它们是附加组件。属性在attrs.xml 中定义,并在themes.xml 和/或styles.xml 中设置。因此,如果您要将自己的可绘制对象分配给selectableItemBackground,则不会使用android: 命名空间。但是,如果可绘制对象是由 Android 系统提供的,你会的。
    • @Vikram 我们如何使用 java 将背景设置为“attr/selectableItemBackground”?
    【解决方案2】:

    这里是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
    

    【讨论】:

    • 所以你建议我将它复制到一个主题或我自己的可绘制对象中,而不是想知道如何将它从支持库中取出来,该支持库应该适用于我的旧 api 级别?
    • 您可以将最小 api 设置为大于 11,否则您必须从 res 文件夹中复制可绘制对象。
    • 我觉得应该可以设置命名空间之类的东西来获取支持库属性
    • 您可以使用 HoloEveryWhere 库,然后它将在 API 7+ 中工作。
    • 我使用的是android support library v7,它的用途和holo一样,假设支持库v7是我唯一的工具,我如何得到相同的结果?
    【解决方案3】:

    不是该主题的专家,但您似乎需要基于平台版本的主题。我认为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" />
    

    希望这会有所帮助。 干杯。

    【讨论】:

    • 好的,这个元素 "?android:attr/selectableItemBackground" 已经在 android support v7 库中了。我不需要从其他库复制主题或制作特定于平台的代码
    • 实际上 kraxor 提出了一个很好的观点,即为不同的 API 级别定制背景。例如,API 21 具有在 21 之前不支持的涟漪效果(即使使用 AppCompat),因此您可能希望以不同的方式设置项目背景
    • 这是一个很好的解决方案,因为它可以在其他视图上重复使用。但是,根据我的经验,样式定义中的属性应该是“android:background”而不是“selectableItemBackground”,如下所示:&lt;style name="LightThemeSelector" parent="android:Theme.Holo.Light"&gt; &lt;item name="android:background"&gt;?android:attr/selectableItemBackground&lt;/item&gt; ... &lt;/style&gt;
    猜你喜欢
    • 1970-01-01
    • 2015-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多