Android GirdView

来源:互联网 发布:软件行业市场调查问卷 编辑:程序博客网 时间:2024/06/07 21:39

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

首先看一下代码:

menu.xml

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns: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.     <include android:id="@+id/menu_title" layout="@layout/title" />  
  8.     <GridView android: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 plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns: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.     <ImageView android: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.     <TextView android: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 plaincopyprint?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <item android:state_pressed="true" android:drawable="@drawable/bg_alibuybutton_selected" />  
  4.     <item android:state_focused="true" android:drawable="@drawable/bg_alibuybutton_selected" />  
  5. </selector>  
在此文件中也可设置default的样式,如果需要的话。


bg_alibuybutton_selected.xml

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <corners android:radius="3dp" />  
  4.     <stroke android:width="0.5dp" android:color="#62809a" />  
  5.     <gradient android: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 plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <corners android:radius="3dp" />  
  4.     <stroke android:width="0.5dp" android:color="#62809a" />  
  5.     <gradient android: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>  


出处:http://blog.csdn.net/weich_java/article/details/6987198

原创粉丝点击