android 实现gridview点击后,带圆角的item内部变色,不是整个矩形变色

首先写一个xml文件  bg_recharge_gd.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!--点击后的item -->
    <item android:state_pressed="true">
        <shape>
            <corners android:radius="@dimen/dp_20px" />
            <solid android:color="#dadada" />
            <stroke android:width="@dimen/dp_2px" android:color="#000000" />
        </shape>
    </item>
    <!--不点击的item -->
    <item>
        <shape>
            <corners android:radius="@dimen/dp_20px" />
            <solid android:color="#eeeeee" />
            <stroke android:width="@dimen/dp_2px" android:color="#000000" />
        </shape>
    </item>
</selector>
在girdview的item布局里把xml文件写入最外层的background里
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="@dimen/dp_302px"
    android:layout_height="@dimen/dp_78px"
    android:background="@drawable/bg_recharge_gd">
    <TextView
        android:id="@+id/gridview_recharge_txt"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textSize="@dimen/dp_32px"
        android:textColor="#000000" />
</LinearLayout>

重点来了!!

最后,需要关掉gridview的默认点击效果,即把gridview的默认点击效果设为透明,不然item的点击效果无效

gridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
完成。

猜你喜欢

转载自blog.csdn.net/pxcz110112/article/details/80513177