Android 为 View 设置简单的点击水波纹效果
效果图(有界水波纹)[越界的是指超过组件范围但不超过父布局范围,不常用]
一、style.xml
,利用属性colorControlHighlight
<style name="MyNoActionBar" parent="Theme.AppCompat.Light.NoActionBar">
<!-- 点击水波纹颜色 -->
<item name="colorControlHighlight">#696969</item>
</style>
然后在需要有水波纹效果的控件中设置android:background
为?attr/selectableItemBackgroundBorderless
android:background="?attr/selectableItemBackgroundBorderless"
二、使用 ripple
文件(对android5.0以上有效)
步骤 ①:在drawable
文件夹下新建一个ripple_item_click.xml
文件,最外层标签为<ripple>
,<ripple>
标签中的color
属性必须填写,这个就是水波纹的颜色;
<?xml version="1.0" encoding="utf-8"?>
<!--View点击水波纹颜色设置,View背景形状设置,选中/未选中设置-->
<ripple
xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#696969">
<!-- 这里相当于普通的shape文件-->
<!-- 这里的shape可以单独提出来,然后这里的item使用drawable属性调用也行-->
<item>
<shape android:shape="rectangle">
<solid android:color="#191919"/>
</shape>
</item>
</ripple>
步骤 ② :将ripple_item_click.xml
设给需要的view
的background
:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="65dp"
<!-- 设置水波纹-->
android:background="@drawable/ripple_item_click"
>