今天我们来模仿一下支付宝钱包首页中带有分割线的GridView,俗称九宫格。先上图,是你想要的效果么?如果是请继续往下看。

我们都知道ListView设置分割线是非常容易的,设置ListView的分割线颜色和宽度,只需要在布局中定义android:divider和android:dividerHeight属性即可。而GridView并没有这样的属性和方法,那我们改如何来做呢?

博主在做这个效果之前,也参考了其他的一些方案,比如说定义一个自定义的GridView,然后在dispatchDraw()方法中在每个item的四周加上一条分割线,这是需要靠算法来实现的,最后这种方法实现的效果并不理想,会出现有些item中没有加上分割线,很难达到我们想要的这种效果。

其实实现这种效果并不难,原理就是让每个item都设置成带有分割线的背景,这样就很容易实现了。

首先我们来写布局:

[html] view plaincopy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:orientation="vertical" >
  5. <ScrollView
  6. android:layout_width="fill_parent"
  7. android:layout_height="wrap_content"
  8. android:fillViewport="true"
  9. android:scrollbars="none" >
  10. <com.finddreams.alipay.MyGridView
  11. android:id="@+id/gridview"
  12. android:layout_width="fill_parent"
  13. android:layout_height="wrap_content"
  14. android:horizontalSpacing="0.0dip"
  15. android:listSelector="@null"
  16. android:numColumns="3"
  17. android:scrollbars="none"
  18. android:stretchMode="columnWidth"
  19. android:verticalSpacing="0.0dip" />
  20. </ScrollView>
  21. </LinearLayout>

因为有时候我们的Gridview中的item可能比较多,为了放得下,一般都会用一个ScrollView来嵌套起来。这时就会出现一个常见的问题,我们在开发中经常会碰到,就是当ListView或者GridView被嵌套在ScrollView中时,发现只会显示第一行的数据,后面的数据就不会显示了。至于产生这个问题的原因,可能是因为Gridview和ListView都是可以根据子item的宽高来显示大小的,但是一旦嵌套到ScrollView中就可以上下滑动,于是系统就不能确定到底该画多大,所以才会产生这样的问题。

这个问题的解决方法在网上很多,一般百度一下就能查到,下面是GridView的解决方法:

[java] view plaincopy
  1. public class MyGridView extends GridView {
  2. public MyGridView(Context context, AttributeSet attrs) {
  3. super(context, attrs);
  4. }
  5. public MyGridView(Context context) {
  6. super(context);
  7. }
  8. public MyGridView(Context context, AttributeSet attrs, int defStyle) {
  9. super(context, attrs, defStyle);
  10. }
  11. @Override
  12. public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  13. int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
  14. MeasureSpec.AT_MOST);
  15. super.onMeasure(widthMeasureSpec, expandSpec);
  16. }
  17. }

接下来,我们就定义一个带分割线的选择器,具体代码是:

[html] view plaincopy
  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"><shape android:shape="rectangle">
  4. <stroke android:width="1.0px" android:color="@color/line" />
  5. <gradient android:angle="270.0" android:endColor="#ffe8ecef" android:startColor="#ffe8ecef" />
  6. </shape></item>
  7. <item android:state_focused="true"><shape android:shape="rectangle">
  8. <gradient android:angle="270.0" android:endColor="#ffe8ecef" android:startColor="#ffe8ecef" />
  9. <stroke android:width="1.0px" android:color="@color/line" />
  10. </shape></item>
  11. <item><shape android:shape="rectangle">
  12. <gradient android:angle="270.0" android:endColor="#ffffffff" android:startColor="#ffffffff" />
  13. <stroke android:width="1.0px" android:color="@color/line" />
  14. </shape></item>
  15. </selector>

定义一个selector,在里面设置一个形状为矩形rectangle,设置这个矩形的stroke描边属性的颜色为分割线的颜色,然后在不同的state的item中设置不同的gradient渐变属性,从而实现在单个item在被点击选中时的效果。

接着就是给我们GridView的item布局中加上背景了:

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:layout_margin="0.0dip"
  6. android:background="@color/griditems_bg" >
  7. <RelativeLayout
  8. android:layout_width="fill_parent"
  9. android:layout_height="fill_parent"
  10. android:layout_centerInParent="true"
  11. android:background="@drawable/bg_gv"
  12. android:padding="12.0dip" >
  13. <ImageView
  14. android:id="@+id/iv_item"
  15. android:layout_width="58.0dip"
  16. android:layout_height="58.0dip"
  17. android:layout_centerHorizontal="true"
  18. android:contentDescription="@string/app_name" />
  19. <TextView
  20. android:id="@+id/tv_item"
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:layout_below="@id/iv_item"
  24. android:layout_centerHorizontal="true"
  25. android:layout_marginTop="5.0dip"
  26. android:maxLines="1"
  27. android:textColor="@color/commo_text_color"
  28. android:textSize="14.0sp" />
  29. </RelativeLayout>
  30. </RelativeLayout>

到这里,就要开始写代码了,定义一个Adapter,把数据填充到GridView中,这一步我想大家都应该都很清楚,这里就不多讲了,不懂的话,可以参考下面的项目代码。

项目链接:http://download.csdn.net/detail/finddreams/8423263    给有需要的朋友!

Android控件GridView之仿支付宝钱包首页带有分割线的GridView九宫格的完美实现相关推荐

  1. Android 控件GridView之仿支付宝钱包首页带有分割线的GridView九宫格的完美实现

    Android控件GridView之仿支付宝钱包首页带有分割线的GridView九宫格的完美实现 关注finddreams:http://blog.csdn.net/finddreams/articl ...

  2. 仿支付宝钱包:带分割线的GridView

    需求: 本文记录了我尝试实现支付宝钱包样式带分割线GridView的过程.首先看一下高大上的支付宝钱包首页: 这里画红框的部分,给人的直观感觉就是一个GridView .当然,这里很可能是支付宝同学自 ...

  3. android 仿旅游日历控件_Android实现仿魅族日历首页功能

    flyme5.0增加了很多优美的动画和交互,界面也变得相当精致.我手头现在就用着魅族MX5,感觉还不错哇!经常会打开它的日历看计划等,感觉它首页的滑动效果还不错,就试着实现一把. 效果分析 1 该首页 ...

  4. android 微信创建群ui,Android控件:高仿微信主UI

    高仿微信主UI 之前在Android组件:Fragment切换后保存状态 一文中讲到了Fragment切换后,是如何保存原来的状态的,最重要的就是用add方法取代现在各种教程常见的replace方法. ...

  5. 自定义控件android.r,Android控件架构与自定义控件

    前言 最近在开发的路上越走越远了,每天在看各位大神公众号更新内容是自定义View的时候,一些小的内容有点模具,决定回过头来温习一下过往的内容.此篇也是根据android群英传来总结的一篇文章. 1 A ...

  6. Android 控件 RecyclerView 看这篇就够了

    [Android 控件 RecyclerView] 概述 RecyclerView是什么 从Android 5.0开始,谷歌公司推出了一个用于大量数据展示的新控件RecylerView,可以用来代替传 ...

  7. Android 控件 RecyclerView

    [Android 控件 RecyclerView] 概述 RecyclerView是什么 从Android 5.0开始,谷歌公司推出了一个用于大量数据展示的新控件RecylerView,可以用来代替传 ...

  8. Android控件架构与自定义控件

    引言 最近在开发的路上越走越远了,每天在看各位大神公众号更新内容是自定义View的时候,一些小的内容有点模具,决定回过头来温习一下过往的内容.此篇也是根据android群英传来总结的一篇文章. 1 A ...

  9. android 代码控件框高,Android控件_TextView(显示文本框控件)

    一.TextView控件的常用属性 1.android:id--控件的id 2.android:layout_width--设置控件的宽度 wrap_content(包裹实际文本内容) fill_pa ...

最新文章

  1. 微软算法100题11 求二叉树中两节点之间的最大距离
  2. 【Android RTMP】音频数据采集编码 ( AAC 音频格式解析 | FLV 音频数据标签解析 | AAC 音频数据标签头 | 音频解码配置信息 )
  3. Lightroom Classic CC8.2安装教程 IT宝盒 10-14 20:32
  4. me)不支持html,属于me的vue练习(参考菜鸟教程).html
  5. 【MySQL】redo log --- 刷入磁盘过程
  6. SQLSERVER复制订阅中的数据库版本选择
  7. Leetcode--5274. 停在原地的方案数
  8. webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode)
  9. Honey Dance I believe
  10. win7主机与linux虚拟机共享方法之右键添加Sharing Options
  11. 优秀Python学习资源收集汇总
  12. 全网首发:怎样制作CDKEY(6)-CDKEY破解
  13. 虚拟机网络配置(配置动态ip和静态ip)
  14. FastDFS搭建图片服务器
  15. 方舟服务器占用多少内存,方舟生存进化占用多少内存
  16. MATALB虚拟魔方构建
  17. javaFx(7)文本阅读器
  18. codeforces 1324 D. Pair of Topics(思维)
  19. 你可能不需要担心,AI对你的工作造成威胁:万字长文解读科技革命与人类发展
  20. 信息系统安全等级保护相关法规及重要国家标准汇总目录

热门文章

  1. 实证研究的步骤_开题报告的研究方法怎么写(最新整理)
  2. 【读点论文】PP-ShiTu: A Practical Lightweight Image Recognition System,百度推出的强大人工智能产品
  3. oc错误:control reaches end of non-void function
  4. windows 环境下node开发环境搭配问题
  5. 记录:mac和win共享磁盘(同一局域网)
  6. 火狐插件油猴Greasemonkey系列二
  7. docker架构的详解
  8. logcat命令使用方法和查看android系统日志缓冲区内容的方法
  9. WG225模块(SDIO WIFI)调试记录
  10. Windows11输入法第一个候选词不显示。