关于水波纹的添加已经定义颜色以及低版本兼容

项目需求 要求在控件上添加点击效果,ok 我本能的想到 我应该是写个选择器,写完之后 产品说应该是水波纹效果,其实在android的版本差异上5.0 之下是没有水波纹的 这个东西是在5.0 及以后才添加的,所以我觉得没必要非加成水波纹,当然在5.0上做成水波纹也可以的,所以我针对系统区分了一下:

5.0 中添加了 波纹效果也就是(ripple)

理论上说 只要使用了Material主题 水波纹点击效果就会应用在所有控件上 5.0上 是用了AppCompat替代了

  1. <resources>  
  2.   <!-- your app's theme inherits from the Material theme -->  
  3.   <style name="AppTheme" parent="android:Theme.Material">  
  4.     <!-- theme customizations -->  
  5.   </style>  
  6. </resources>
  1. <resources>  
  2.   
  3.     <!-- Base application theme. -->  
  4.     <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">  
  5.         <!-- Customize your theme here. -->  
  6.         <item name="colorPrimary">@color/colorPrimary</item>  
  7.         <item name="colorPrimaryDark">@color/colorPrimaryDark</item>  
  8.         <item name="colorAccent">@color/colorAccent</item>  
  9.     </style>  
  10.   
  11. </resources> 

系统自带的有两种一种无界 一种有界 只要设置background就可以了

有界:android:background="?android:attr/selectableItemBackground"

无界:android:background="?android:attr/selectableItemBackgroundBorderless"  安装最低版本在21 所以target在21

然而这并不能满足我的需求所以只能另谋蹊径了

自定义 5.0 下用selecter 以上用ripple

因为ripple 在5.0以上才有所以我在5.0上使用drawable-v21就是在drawable同级目录下新建一个

在5.0 一下用drawable中的资源

为了避免引用失败这种低级错误 在drawable 与 drawable-v21中的名称应该相同哦 就叫click_shape_bg 因为定义了圆角所以带个shape

drawable

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="false">
        <shape android:shape="rectangle">
            <corners android:radius="5dp" />
            <solid android:color="@color/colorPrimary" />
        </shape>
    </item>
    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <corners android:radius="5dp" />
            <solid android:color="@color/colorPrimaryDark" />
        </shape>
    </item>
</selector>
drawable-v21

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/colorPrimaryDark">
    <!--上面的颜色是水波纹的颜色-->
    <!--去掉此item内容就是无界效果-->
    <item>
        <!--背景色设置圆角等操作-->
        <shape android:shape="rectangle">
            <solid android:color="@color/colorPrimary"/>
            <corners android:radius="5dp"></corners>
        </shape>
    </item>
</ripple>
应该要上个效果的 我想想怎么传一个视频啊


下载地址点击打开链接


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值