Android 为 View 设置简单的点击水波纹效果

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设给需要的viewbackground:

<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"        
    >

猜你喜欢

转载自blog.csdn.net/C_biubiubiu/article/details/110002761