Ripple是Material Design(材料设计)中的效果;
虽然在5.0的机型上,会自带Ripple点击效果,但是有时候需要自己更改点击效果;
使用Ripple的关键就是在android:background中设置;
使用方法也非常简单,在drawable中创建**.xml文件:
<!--在真是项目中都有自己的主题颜色,Ripple效果的颜色默认是灰色;
可通过android:color 这个属性改变涟漪波浪的颜色-->
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:color="#ffff0000"
tools:targetApi="lollipop">>
<!--下面设置颜色没有用,添加item以后会约束水波纹的范围-->
<item android:drawable="@android:color/white" />
</ripple>
直接在需要水波纹的按钮上面设置:
<!--直接设置android:background="@drawable/ripple_01"属性即可
记住必须添加android:clickable="true"属性,不然不生效-->
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_margin="70dp"
android:background="@drawable/ripple_01"
android:clickable="true"
android:text="不限定边界" />
但是很多按钮需要自定义shape或者设置UI给的图片,在Ripple中设置图片也非常的简单:
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#FF21272B">
<!--自定义的shape作为背景
也可添加一张图片作为背景-->
<item android:drawable="@drawable/tv_bg" />
</ripple>
shape文件tv_ba.xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#d5d555" />
<corners android:radius="4dp" />
</shape>
点击打开链接下载源码