from:http://blog.csdn.net/weich_java/article/details/6987198

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

首先看一下代码:

menu.xml

[html] view plain copy
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:background="#e1e7e8"
  6. android:orientation="vertical">
  7. <includeandroid:id="@+id/menu_title"layout="@layout/title"/>
  8. <GridViewandroid:listSelector="#e1e7e8"
  9. android:id="@+id/menu"android:layout_width="fill_parent"android:layout_marginTop="10dip"
  10. android:layout_height="fill_parent"android:numColumns="auto_fit"
  11. android:verticalSpacing="10dp"android:horizontalSpacing="10dp"
  12. android:columnWidth="90dp"android:stretchMode="columnWidth"
  13. android:gravity="center">
  14. </GridView>
  15. </LinearLayout>
其中android:listSelector="#e1e7e8"这句,android:listSelector的颜色值一定要和它父类容器的背景色相同,这样就不会出现选中时的黑色。

menuitem.xml

[html] view plain copy
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_height="wrap_content"
  4. android:paddingBottom="4dip"
  5. android:background="@drawable/bg_alibuymenu_states"
  6. android:layout_width="fill_parent">
  7. <ImageViewandroid:layout_height="wrap_content"
  8. android:id="@+id/ItemImage"
  9. android:layout_marginTop="20dip"
  10. android:layout_width="wrap_content"
  11. android:layout_centerHorizontal="true">
  12. </ImageView>
  13. <TextViewandroid:layout_width="wrap_content"
  14. android:gravity="center"
  15. android:textColor="@color/text_color"
  16. android:singleLine="true"
  17. android:textSize="16dip"
  18. android:layout_below="@+id/ItemImage"
  19. android:layout_height="wrap_content"
  20. android:layout_centerHorizontal="true"
  21. android:id="@+id/ItemText">
  22. </TextView>
  23. </RelativeLayout>

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

bg_alibuymenu_states.xml

[html] view plain copy
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <selectorxmlns:android="http://schemas.android.com/apk/res/android">
  3. <itemandroid:state_pressed="true"android:drawable="@drawable/bg_alibuybutton_selected"/>
  4. <itemandroid:state_focused="true"android:drawable="@drawable/bg_alibuybutton_selected"/>
  5. </selector>
在此文件中也可设置default的样式,如果需要的话。


bg_alibuybutton_selected.xml

[html] view plain copy
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <shapexmlns:android="http://schemas.android.com/apk/res/android">
  3. <cornersandroid:radius="3dp"/>
  4. <strokeandroid:width="0.5dp"android:color="#62809a"/>
  5. <gradientandroid:startColor="@color/button_selected_start_color"
  6. android:endColor="@color/button_selected_end_color"android:type="linear"
  7. android:angle="90"android:centerX="0.5"android:centerY="0.5"/>
  8. </shape>

bg_alibuybutton_default.xml

[html] view plain copy
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <shapexmlns:android="http://schemas.android.com/apk/res/android">
  3. <cornersandroid:radius="3dp"/>
  4. <strokeandroid:width="0.5dp"android:color="#62809a"/>
  5. <gradientandroid:startColor="@color/button_defalut_start_color"
  6. android:endColor="@color/button_defalut_end_color"android:type="linear"
  7. android:angle="90"android:centerX="0.5"android:centerY="0.5"/>
  8. </shape>

更多相关文章

  1. Android(安卓)ListView Adapter的getItemViewType和getViewTypeC
  2. Android(安卓)NDK 编译时出现make (e=2): 系统找不到指定的文
  3. 第一行代码Android笔记精华版
  4. 关于Android(安卓)TabHost切换Tab字体的颜色背景颜色改变
  5. Ubuntu10.10下编译Android2.2内核
  6. Android经典底部选项卡集成方式之一
  7. Android调取拍照和获取拍照后的照片
  8. 下拉列表 spinner(android)
  9. android RadioGroup实现单选以及默认选中

随机推荐

  1. 开博第一日
  2. Android结合Retrofit实现统一加解密处理(G
  3. 深入探索Android稳定性优化
  4. Android自定义视图
  5. Android与WebView的同步和异步访问机制
  6. Android消息循环的同步屏障机制及UI渲染
  7. android中的下载问题
  8. Activity生命周期详解
  9. 【较详细】Andriod 从源码的角度详解View
  10. Android java层音频相关的分析与理解(二)音