Android sets a simple click water ripple effect for View
效果图(有界水波纹)[越界的是指超过组件范围但不超过父布局范围,不常用]
1. style.xml
Use attributescolorControlHighlight
<style name="MyNoActionBar" parent="Theme.AppCompat.Light.NoActionBar">
<!-- 点击水波纹颜色 -->
<item name="colorControlHighlight">#696969</item>
</style>
Then set it android:background
to the control that needs water ripple effect?attr/selectableItemBackgroundBorderless
android:background="?attr/selectableItemBackgroundBorderless"
Second, the use of ripple
file (valid for android5.0 above)
Step ①: drawable
Create a new file under the folder ripple_item_click.xml
, the outermost label is <ripple>
, <ripple>
the color
attributes in the label must be filled in, this is the color of the water ripple;
<?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>
Step ②: the ripple_item_click.xml
set to the required view
of 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"
>