Android 为什么不是';我的ImageButton样式是否应用于我的ImageButton?

Android 为什么不是';我的ImageButton样式是否应用于我的ImageButton?,android,android-styles,Android,Android Styles,我已经设置了一些样式来处理不同的API(一个用于v21,另一个用于低于v21的任何API)。我想为我的ImageButton设计一个样式,但它似乎没有按照我期望的方式工作 v-21的样式是 <style name="BorderlessImageButton" parent="AppTheme.BorderlessImageButton"> <item name="android:background">?android:attr/selectableItemBa

我已经设置了一些样式来处理不同的API(一个用于v21,另一个用于低于v21的任何API)。我想为我的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>

?android:attr/SelectableItemBackgroundless无边界
@颜色/辅助文本
将用于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/SelectableItemBackgroundless时预期的那样对我的触摸做出视觉反应。此外,在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>
res/color/borderless_button_background.xml:
请帮助我让我的按钮根据用户设备上的API对触摸做出正确反应


谢谢

您需要用

style="@style/BorderlessImageButton"
属性
android:theme
用于活动,请参阅


更新


由于Android 5.0或使用AppCompat 22.1.0+(api 11+),您还可以在单个视图上使用
ThemeOverlay
Android:theme
。您可以阅读有关此技术的信息。多亏了伊恩。

你用
style=“@style/BorderlessImageButton”
应用你的风格,
android:theme
是活动的主题,就是这样。回答吧,伙计!我会把它标记为完成。我还放了一个与文档的链接,这也很有帮助:-)非常感谢。我一直在思考这样的样式视图,但似乎我没有正确地应用它。事实上,您可以在单个视图上使用
android:theme
,如中所述。然而,这些不使用样式,但理想情况下是一个
ThemeOverlay
,它更接近活动样式的模型。谢谢,我把它作为未来读者的答案。我也在博客上写了一条小评论。