第一种方法:
直接在Item View中的LinearLayout /or CardView 添加代码–》
xml代码:
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
第二种方法:
在res/drawable 和 res/drawable-v21 中分别添加item_selector.xml –》
res/ drawable/ item_selector.xml 代码:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/text_Grey" android:state_pressed="true"/>
<item android:drawable="@color/text_Grey" android:state_focused="true"/>
<item android:drawable="@color/colorWhite"/>
</selector>
res/ drawable-v21/ item_selector.xml 代码:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/text_Grey">
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="1dp" />
<solid android:color="@color/colorWhite" />
</shape>
</item>
</ripple>
colors.xml 代码:
<color name="text_Grey">#8496a0</color>
<color name="colorWhite">#ffffff</color>
然后在Item View中的LinearLayout /or CardView 添加代码–》
xml代码:
android:background="@drawable/item_selector"
android:clickable="true"
android:focusable="true"
Button 完美效果
res/ drawable-v21/ item_selector.xml 代码:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/btn_white"/>
<corners android:radius="4dp"/>
</shape>
</item>
<item android:drawable="?android:selectableItemBackground"/>
</layer-list>