Android GridView选择样式

笔者在用GridView时发现GridView的select style会根据系统而不同,因为在客户端中一边具有统一的显示风格,所以尝试了下指定GridView的选中样式。

首先看一下代码:

menu.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:background="#e1e7e8"
	android:orientation="vertical">
	<include android:id="@+id/menu_title" layout="@layout/title" />
    <GridView android:listSelector="#e1e7e8"
	android:id="@+id/menu" android:layout_width="fill_parent" android:layout_marginTop="10dip"
	android:layout_height="fill_parent" android:numColumns="auto_fit"
	android:verticalSpacing="10dp" android:horizontalSpacing="10dp"
	android:columnWidth="90dp" android:stretchMode="columnWidth"
    android:gravity="center">
    </GridView>
</LinearLayout>
其中android:listSelector="#e1e7e8"这句,android:listSelector的颜色值一定要和它父类容器的背景色相同,这样就不会出现选中时的黑色。

menuitem.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="4dip"
	android:background="@drawable/bg_alibuymenu_states"
	android:layout_width="fill_parent">
	<ImageView android:layout_height="wrap_content" 
	    android:id="@+id/ItemImage"
	    android:layout_marginTop="20dip"
		android:layout_width="wrap_content" 
		android:layout_centerHorizontal="true">
	</ImageView>
	<TextView android:layout_width="wrap_content"
	    android:gravity="center"
	    android:textColor="@color/text_color"
	    android:singleLine="true"
	    android:textSize="16dip"
		android:layout_below="@+id/ItemImage" 
		android:layout_height="wrap_content"
		android:layout_centerHorizontal="true" 
		android:id="@+id/ItemText">
	</TextView>
</RelativeLayout>

android:background="@drawable/bg_alibuymenu_states"这句话指定了 GridView中的元素的背景风格。


bg_alibuymenu_states.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/bg_alibuybutton_selected" />
	<item android:state_focused="true" android:drawable="@drawable/bg_alibuybutton_selected" />
</selector>
在此文件中也可设置default的样式,如果需要的话。


bg_alibuybutton_selected.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
	<corners android:radius="3dp" />
	<stroke android:width="0.5dp" android:color="#62809a" />
	<gradient android:startColor="@color/button_selected_start_color"
		android:endColor="@color/button_selected_end_color" android:type="linear"
		android:angle="90" android:centerX="0.5" android:centerY="0.5" />
</shape>

bg_alibuybutton_default.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
	<corners android:radius="3dp" />
	<stroke android:width="0.5dp" android:color="#62809a" />
	<gradient android:startColor="@color/button_defalut_start_color"
		android:endColor="@color/button_defalut_end_color" android:type="linear"
		android:angle="90" android:centerX="0.5" android:centerY="0.5" />
</shape>



  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值