Android GirdView

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

首先看一下代码:

menu.xml

[html] view plain copy print ?
  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>  
<?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

[html] view plain copy print ?
  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>  
<?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

[html] view plain copy print ?
  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>  
<?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

[html] view plain copy print ?
  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>  
<?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

[html] view plain copy print ?
  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>  

<?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>
Android GirdView_第1张图片

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

你可能感兴趣的:(html,android,layout,encoding)