Android的GridView中的选中图标后图标的背景颜色

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>



在drawable文件夹下创建按钮选中,获取焦点,默认状态和颜色之间的对应关系

<?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"时,表示引用的是一张图片,也就是说按下的时候是一张图片,松开的时候是另一张图片,这时候是图片之间的切换了而不是颜色之间的切换了。)


效果如下:




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值