我可以帮你进行翻译。这段内容涉及到 IT 技术,包含了样式设置和 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>
所有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
则用于活动。 - Gil Vegliach