笔者在用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>



更多相关文章

  1. Android中Intent的使用示例
  2. Android定制RadioButton样式三种实现方法
  3. android输入框一条线和一个框两种样式的设定
  4. Android(六)通知、样式、主题、HTML
  5. 在android的学习中遇到的一些小问题
  6. [置顶] android四大组件之intent
  7. Android指定专用APN
  8. android Intent机制详解
  9. [基础知识]全面认识AndroidManifest.xml文件(二)

随机推荐

  1. android如何往SDCard中存取图片
  2. Java如何操作Android的adb shell 之 我自
  3. Android——XML解析
  4. Android中的常见FC及解决方式整理
  5. android 分享 api
  6. Android消息机制 Handler
  7. android 实现由下至上弹出并位于屏幕底部
  8. Android(安卓)GPS状态改变与监听
  9. android 设置静态wifi地址
  10. Android(安卓)软键盘小知识点