我在Android Studio中使用Android API 21。
我改变了按钮的背景颜色。它停止提供早先给出的触摸反馈。
我一直在寻找解决方案,并找到了创建drawables
的方法,但是我在Android开发者网站上阅读了有关触摸反馈的内容,我发现了这些:
https://developer.android.com/design/style/touch-feedback.html
合并您的品牌更容易,因为默认的触摸反馈适用于您选择的任何色调。
和
https://developer.android.com/design/style/branding.html
在自定义颜色时,触摸反馈应该是微妙的 - 比未触摸的颜色略微更浅或更暗。
这是否意味着,即使我自动更改了背景,触摸反馈也会起作用?或者我是否需要做一些工作才能使其正常工作?
此外,如果我需要手动重现反馈,我该怎么做(比最新Android Studio中的drawable更好的方法)?
刚开始使用android,所以非常困惑。感谢您的帮助。谢谢。
答案 0 :(得分:1)
您需要在drawable中明确设置触摸反馈(按钮按状态)。改变背景意味着失去反馈(即按下)状态。
切换按钮背景及其反馈/按下状态的唯一方法是在Drawables中定义各种按钮定义。这样,只需一个参考按钮即可轻松切换背景(按钮,按下,聚焦和启用)。
方法setBackgroundResource()仅将引用的资源设置为 all Button的状态。因此,最好在Drawable中定义一个Button定义及其所有状态,并使用它们进行切换。同样适用于setBackgroundColor(),它应用背景颜色覆盖按钮的所有状态,并且只应在需要时使用(对于大多数情况,不应使用它)。
我不使用Android Studio。我使用旧的基于Eclipse的ADT,但是,我确定没有使用Drawables的解决方法,因为Android应用程序开发在各种IDE上都是标准化的按钮状态和背景。
通过drawables实现Button定义非常容易,例如:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Button Disabled -->
<item
android:state_enabled="false">
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient
android:startColor="#F4F4F4"
android:centerColor="#A6A6A6"
android:endColor="#F4F4F4"
android:angle="90"/>
<padding android:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="7dp" />
<stroke
android:width="2dip"
android:color="#FFFFFF" />
<corners android:radius= "8dp" />
</shape>
</item>
<!-- Button Pressed / Feedback -->
<item
android:state_pressed="true"
android:state_enabled="true">
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient
android:startColor="#2222EE"
android:centerColor="#22EE11"
android:endColor="#2222EE"
android:angle="90"/>
<padding android:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="7dp" />
<stroke
android:width="2dip"
android:color="#FFFFFF" />
<corners android:radius= "8dp" />
</shape>
</item>
<!-- Button Focused -->
<!-- Sometimes developers define Shapes in a separate drawable. The code is the same. Here's an example. -->
<item
android:state_focused="true"
android:state_enabled="true"
android:drawable="@drawable/button_focused"> <!-- Refers to another drawable file with name "button_focused" -->
</item>
<!-- Button Enabled -->
<item
android:state_enabled="true">
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient
android:startColor="#22EE11"
android:centerColor="#2222EE"
android:endColor="#22EE11"
android:angle="90"/>
<padding android:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="7dp" />
<stroke
android:width="2dip"
android:color="#FFFFFF" />
<corners android:radius= "8dp" />
</shape>
</item>
</selector>
将您的Button定义拆分为多个drawable具有它的优点和缺点。将一个Button定义保存在一个drawable中会更好。
祝你好运!
答案 1 :(得分:0)
最简单的解决方案是 代替
mode
使用此行
android:background="@color/myColor"