为什么我的 ImageButton 样式没有应用到我的 ImageButton?

Why isn't my ImageButton style being applied to my ImageButton?

我设置了一些样式以在不同的 API 上工作(一种用于 v21,另一种用于任何低于该版本的版本)。我想为我的 ImageButton 设计一种样式,但它似乎没有按我预期的方式工作。

v-21 的款式是

<style name="BorderlessImageButton" parent="AppTheme.BorderlessImageButton">
    <item name="android:background">?android:attr/selectableItemBackgroundBorderless</item>
    <item name="android:tint">@color/secondary_text</item>
</style>

将用于 v-21 以下所有其他 API 的样式是

<style name="BorderlessImageButton" parent="AppTheme.BorderlessImageButton"/>

<style name="AppTheme.BorderlessImageButton" parent="@android:style/Widget.ImageButton">
    <item name="android:tint">@color/secondary_text</item>
    <item name="android:background">@color/borderless_button_background</item>
</style>

这是我的 xml 资源

<ImageButton
    android:id="@+id/imageButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_toStartOf="@id/date_picker"
    android:background="@null"
    android:padding="10dp"
    android:src="@drawable/ic_today_white_24dp"
    android:theme="@style/BorderlessImageButton" />

如果我 运行 在具有 v-21 或 v-22 的设备上使用此按钮,按钮不会像我预期的那样对我的触摸做出视觉反应?android:attr/selectableItemBackgroundBorderless。此外,在 v-21 以下的任何设备上,按钮仍然不会对我为其设置的选择器资源做出反应。

res/color/borderless_button_background.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="@color/accent" />
    <item android:color="@color/transparent"/>
</selector>

请帮助我让我的按钮根据 API 用户在其设备上的触摸做出正确反应。

谢谢

您需要应用您的风格

style="@style/BorderlessImageButton"

属性 android:theme 用于活动,参见 this


更新

自 Android 5.0 起,或使用 AppCompat 22.1.0+ (api 11+),您还可以使用 ThemeOverlay 并在单个视图上使用 android:theme .您可以阅读此技术 here。感谢伊恩。