美文网首页
Android--Button自定义水波纹颜色方法

Android--Button自定义水波纹颜色方法

作者: aruba | 来源:发表于2020-02-26 14:46 被阅读0次
我们在使用Button时,默认点击会带有一个水波纹扩散的效果,如果我们想要使用自己的颜色,那怎么办呢,今天就来介绍二种实现自定义颜色水波纹的方法
方法一:使用drawable

在drawable-v21中新建selector_ripple.xml

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/colorPrimaryDark">
    <item android:drawable="@color/colorPrimary" />
</ripple>
使用该方法时,在安卓5.0以下系统中会崩溃,因为5.0以下不支持水波纹效果,所以我们需要在drawable中创建一个同名的xml,来兼容5.0以下

在drawable中新建selector_ripple.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/colorPrimaryDark" android:state_pressed="true"/>
    <item android:drawable="@color/colorPrimary" />
</selector>
然后再xml中给button设置background
       <Button
            android:id="@+id/btn_next1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/selector_ripple"
            android:text="next" />
方法二:使用Theme

在styles中,自定义style

    <style name="MyButton" parent="Theme.AppCompat.Light">
        <item name="colorButtonNormal">@color/colorPrimary</item>
        <item name="colorControlHighlight">@color/colorPrimaryDark</item>
    </style>
然后再xml中给button设置theme
        <Button
            android:id="@+id/btn_next1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:theme="@style/MyButton"
            android:text="next" />
这种方法会自动兼容5.0以下,使用也更便洁

相关文章

  • Android--Button自定义水波纹颜色方法

    我们在使用Button时,默认点击会带有一个水波纹扩散的效果,如果我们想要使用自己的颜色,那怎么办呢,今天就来介绍...

  • 3--UIColor

    大纲: 使用类方法获取系统的颜色 自定义颜色的方法 开发小技巧 一、使用类方法获取系统的颜色 二、自定义颜色的方法...

  • ripple水波纹 和 Palette调色板 了解

    ripple 水波纹 5.0之后按钮会自带水波纹效果,但是颜色是固定的。而且如果给按钮设置了背景后,水波纹效果就没...

  • iOS 设置背景色

    设置背景色 UIColor 常用颜色方法如下: 自定义颜色 希望对你有帮助!

  • View -- Paint

    整理精简, 学习自定义view中paint的使用方法 颜色 效果 颜色 基本颜色绘制 setColor(int c...

  • 开源View特效

    自定义WaveProgressView满足你所有水波纹加载需求 贝塞尔曲线开发的艺术 自定义AvatarImage...

  • SwiftUI(4)Color

    16进制颜色使用 Color扩展方法 展示: 自定义颜色 设置名称,暗黑模式色值 使用: 展示:

  • Android进阶-view 的事件分发机制

    在前面的文章中有一系列的自定义 view 的文章,Android自定义View-水波纹progressbarAnd...

  • iOS tableView刷新指定 Cell 指定分区 自定义

    点击 Cell 颜色恢复在 didselect 代理方法里面添加 自定义 Cell 点击时候的颜色在初始化 Cel...

  • 完全自定义控件-自定义绚丽水波纹效果

    自定义绚丽水波纹效果 效果展示 需求 模拟水波纹的效果:点击屏幕就有圆环出现,半径从小到大,透明度从大到小(0为透...

网友评论

      本文标题:Android--Button自定义水波纹颜色方法

      本文链接:https://www.haomeiwen.com/subject/yilvchtx.html