笔者在用GridView时发现GridView的select style会根据系统而不同,因为在客户端中一边具有统一的显示风格,所以尝试了下指定GridView的选中样式。
首先看一下代码:
menu.xml
- <?xmlversion="1.0"encoding="utf-8"?>
-
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent"
-
android:background="#e1e7e8"
-
android:orientation="vertical">
-
<includeandroid:id="@+id/menu_title"layout="@layout/title"/>
-
<GridViewandroid: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
- <?xmlversion="1.0"encoding="utf-8"?>
-
<RelativeLayoutxmlns: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">
-
<ImageViewandroid:layout_height="wrap_content"
-
android:id="@+id/ItemImage"
-
android:layout_marginTop="20dip"
-
android:layout_width="wrap_content"
-
android:layout_centerHorizontal="true">
-
</ImageView>
-
<TextViewandroid: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
- <?xmlversion="1.0"encoding="UTF-8"?>
-
<selectorxmlns:android="http://schemas.android.com/apk/res/android">
-
<itemandroid:state_pressed="true"android:drawable="@drawable/bg_alibuybutton_selected"/>
-
<itemandroid:state_focused="true"android:drawable="@drawable/bg_alibuybutton_selected"/>
-
</selector>
在此文件中也可设置default的样式,如果需要的话。
bg_alibuybutton_selected.xml
- <?xmlversion="1.0"encoding="utf-8"?>
-
<shapexmlns:android="http://schemas.android.com/apk/res/android">
-
<cornersandroid:radius="3dp"/>
-
<strokeandroid:width="0.5dp"android:color="#62809a"/>
-
<gradientandroid: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
- <?xmlversion="1.0"encoding="utf-8"?>
-
<shapexmlns:android="http://schemas.android.com/apk/res/android">
-
<cornersandroid:radius="3dp"/>
-
<strokeandroid:width="0.5dp"android:color="#62809a"/>
-
<gradientandroid: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>
出处:http://blog.csdn.net/weich_java/article/details/6987198
分享到:
相关推荐
android Gridview分页实现的一个小例子
Android GridView 点击缩放图像
android gridview分页和滑动效
Android GridView使用例子
Android GridView完美横向滑动 ,并且可限制Gridview显示行数!
android gridview多选删除工程,支持同时选定多个item然后删除
Android GridView分组显示、带dock标题,带下拉刷新
android GridView 源代码 根据以下博客制作 http://blog.csdn.net/hellogv/article/details/4567095
一个非常好的 android gridview 异步加载图片的例子,需要17以上的api
android gridview adapter 实例 每个getview显示不同的数据
Android GridView控件的学习 博客地址:http://blog.csdn.net/csdnyuandaimaxuexi/article/details/48440203
AndroidGridView点击每一个图片进入个人页面渐变特效.zip,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
android gridview 上图下字
android GridView分组 convertview可重用 可用于 图片相册 日期分组 内存消耗低
android gridview 加载大量图片。无OOM。 程序运行默认加载SD卡内所有图片文件,这是我写了之后用在一款成熟app里面的。可用
Android GridView + ViewFlipper布局界面,模仿“机锋市场.rar
An Android GridView that can be configured to scroll horizontally or vertically. I should have posted this over a year and a half ago, but never got around to it. I needed a grid view that in portrait...
Android GridView,Gallery动态更新数据(整理自网络)
Android GridView 多标题分组显示,上下拉刷新,convertview可重用 效率高,不卡
Android GridView + ViewFlipper布局界面,模仿“机锋市场.