from:http://blog.csdn.net/weich_java/article/details/6987198
笔者在用GridView时发现GridView的select style会根据系统而不同,因为在客户端中一边具有统一的显示风格,所以尝试了下指定GridView的选中样式。
首先看一下代码:
menu.xml
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#e1e7e8"
android:orientation="vertical">
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">
其中android:listSelector="#e1e7e8"这句,android:listSelector的颜色值一定要和它父类容器的背景色相同,这样就不会出现选中时的黑色。
menuitem.xml
android:layout_height="wrap_content"
android:paddingBottom="4dip"
android:background="@drawable/bg_alibuymenu_states"
android:layout_width="fill_parent">
android:id="@+id/ItemImage"
android:layout_marginTop="20dip"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true">
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">
android:background="@drawable/bg_alibuymenu_states"这句话指定了 GridView中的元素的背景风格。
bg_alibuymenu_states.xml
在此文件中也可设置default的样式,如果需要的话。
bg_alibuybutton_selected.xml
android:endColor="@color/button_selected_end_color"android:type="linear"
android:angle="90"android:centerX="0.5"android:centerY="0.5"/>
bg_alibuybutton_default.xml
android:endColor="@color/button_defalut_end_color"android:type="linear"
android:angle="90"android:centerX="0.5"android:centerY="0.5"/>