背景
Android5.0以上新增了波纹效果
在已有项目中没有使用AppCompatActivity,所以在item最外层容器中增加
android:foreground="?attr/selectableItemBackground"
之后,波纹效果依然无效
解决办法
在drawable-v21中新建一个xml文件item_spot_touch_bg.xml,代码如下
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/caldroid_lighter_gray">
<item android:drawable="@color/white"/>
</ripple>
在itemview的最外层容器中,设置background为上述xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:foreground="?android:attr/selectableItemBackground"
android:background="@drawable/item_spot_touch_bg" >
...
</LinearLayout>
注意事项
为了兼容Android5.0以下的系统,需要在drawable新建一个同名的xml文件item_spot_touch_bg.xml,代码如下
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/caldroid_lighter_gray" android:state_enabled="true" android:state_pressed="true"/>
<item android:drawable="@color/white" android:state_enabled="true" android:state_pressed="false"/>
<item android:drawable="@color/white"></item>
</selector>
网友评论