android gridview按钮边框和定制点击颜色

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="2"
android:verticalSpacing="2dp"
android:horizontalSpacing="2dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:background="#11000000"
android:listSelector="@drawable/grid_view_item_selector_xml"
/>

gridView的布局文件:grid_view_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:layout_width="fill_parent"
android:background="@drawable/grid_view_item_selector_xml"
> <ImageView
android:layout_height="wrap_content"
android:id="@+id/ItemImage"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true">
</ImageView> <TextView
android:layout_width="wrap_content"
android:layout_below="@id/ItemImage"
android:layout_height="wrap_content"
android:text="TextView01"
android:layout_centerHorizontal="true"
android:id="@+id/ItemText">
</TextView>
</RelativeLayout>

grid_view_item_selector_xml.xml

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true" android:drawable="@drawable/grid_view_item_press_shape"/>

    <item android:drawable="@drawable/grid_view_item_shape"/>
</selector>

grid_view_item_press_shape.xml

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#5500ffff"/>
</shape>

grid_view_item_shape.xml

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#fff"/>
</shape>

注意:在gridView组件中要声明listSelector来覆盖原有的点击颜色效果,还有在gridView中声明该item的背景颜色。

上一篇:spring aop 源码分析(三) @Scope注解创建代理对象


下一篇:Spring AOP源码分析(三):基于JDK动态代理和CGLIB创建代理对象的实现原理