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的背景颜色。

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。