我們?cè)谑褂肂utton時(shí),默認(rèn)點(diǎn)擊會(huì)帶有一個(gè)水波紋擴(kuò)散的效果熟呛,如果我們想要使用自己的顏色广辰,那怎么辦呢,今天就來(lái)介紹二種實(shí)現(xiàn)自定義顏色水波紋的方法
方法一:使用drawable
在drawable-v21中新建selector_ripple.xml
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/colorPrimaryDark">
<item android:drawable="@color/colorPrimary" />
</ripple>
使用該方法時(shí)蔗崎,在安卓5.0以下系統(tǒng)中會(huì)崩潰,因?yàn)?.0以下不支持水波紋效果扰藕,所以我們需要在drawable中創(chuàng)建一個(gè)同名的xml缓苛,來(lái)兼容5.0以下
在drawable中新建selector_ripple.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/colorPrimaryDark" android:state_pressed="true"/>
<item android:drawable="@color/colorPrimary" />
</selector>
然后再xml中給button設(shè)置background
<Button
android:id="@+id/btn_next1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/selector_ripple"
android:text="next" />
方法二:使用Theme
在styles中,自定義style
<style name="MyButton" parent="Theme.AppCompat.Light">
<item name="colorButtonNormal">@color/colorPrimary</item>
<item name="colorControlHighlight">@color/colorPrimaryDark</item>
</style>
然后再xml中給button設(shè)置theme
<Button
android:id="@+id/btn_next1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="@style/MyButton"
android:text="next" />