Android sets a simple click water ripple effect for View

Android sets a simple click water ripple effect for View


效果图(有界水波纹)[越界的是指超过组件范围但不超过父布局范围,不常用]

Insert picture description here

1. style.xmlUse attributescolorControlHighlight

<style name="MyNoActionBar" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- 点击水波纹颜色 -->
    <item name="colorControlHighlight">#696969</item>
</style>

Then set it android:backgroundto the control that needs water ripple effect?attr/selectableItemBackgroundBorderless

android:background="?attr/selectableItemBackgroundBorderless"

Second, the use of ripplefile (valid for android5.0 above)

Step ①: drawableCreate a new file under the folder ripple_item_click.xml, the outermost label is <ripple>, <ripple>the colorattributes 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.xmlset to the required viewof 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"        
    >

Guess you like

Origin blog.csdn.net/C_biubiubiu/article/details/110002761