解决 Button 设置 background 之后点击动画效果消失的问题

来源:互联网 发布:od跳过网络验证 编辑:程序博客网 时间:2024/05/17 01:01

文章已同步至简书:http://www.jianshu.com/p/064fbf8a1fa5

在 Android 5.0 推出之后,点击 Button 默认增加了水波纹的动画效果。但是按照往常的方式给 Button 设置了 background 之后,发现水波纹效果没有了。

四角尖尖,而且点击时给人感觉很突兀。

不过到了现在我才知道是添加颜色的方式不对。

正确的改变 Button 颜色的姿势如下:

首先在 values/styles.xml 文件中添加如下风格:

<style name="BlueButtonStyle" parent="ThemeOverlay.AppCompat">    <item name="colorButtonNormal">@android:color/holo_blue_light</item></style>

或者是:

<style name="RedButtonStyle" parent="Widget.AppCompat.Button.Borderless">    <item name="colorButtonNormal">@android:color/holo_red_light</item></style>

这两种主题都可以,尝试之后,发现效果一致。

在 xml 文件中使用:

<Button    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:layout_margin="16dp"    android:text="Button"    android:textAllCaps="false"    android:textColor="@android:color/white"    android:theme="@style/RedButtonStyle"/>

水波纹效果出来了。

其中有一行 android:textAllCaps="false" 可以解除 Button 对文本大写的限制。

阅读全文
1 0
原创粉丝点击