1.首先在GridView组件中添加android:listSelector="@android:color/transparent",表示选中图标后颜色是透明的(给用户的感觉就是根本不晓得点没点图标)
2.在drawable文件夹下创建item选中,获取焦点,默认,这些状态和颜色的对应关系的配置文件
3.在GridView中item的布局文件中引入第二步创建的配置文件android:background="@drawable/home_selector"
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<GridView
android:id="@+id/haha"
android:numColumns="3"
android:listSelector="@android:color/transparent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</GridView>
</LinearLayout>
item.xml(GridView中的组件的布局配置文件)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/seletced"
android:orientation="vertical" ><!--android:background="@drawable/seletced"导入最后那个状态和颜色之间对应关系的配置文件 -->
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/weishi"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="哈哈哈哈"
/>
</LinearLayout>
在valuse文件夹下创建颜色的资源文件(就是颜色名称和颜色的值对应)
color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="trans">@android:color/transparent</color><!-- 透明 -->
<color name="green">#33FF33</color>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/green" android:state_pressed="true"/><!-- @color表示的是color.xml green表示name=green的标签里的属性 -->
<!-- pressed -->
<item android:drawable="@color/green" android:state_focused="true"/>
<!-- focused -->
<item android:drawable="@color/trans"/>
<!-- default -->
</selector>
在布局文件中引入 状态和颜色之间对应的xml。
在状态和颜色之间对应的xml中引用color.xml中定义的颜色(这一步可换成图片引用,因为当item android:drawable="@drawable/button_green_on"时,表示引用的是一张图片,也就是说按下的时候是一张图片,松开的时候是另一张图片,这时候是图片之间的切换了而不是颜色之间的切换了。)