经常有这样的需求,你的功能图标要像一个个物品,摆放在书架上,像这样:

我的思路比较简单,重载GridView,在他绘制子视图前,先把背景绘制完成

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class RowGridView extends GridView {
    private static String NAMESPACE_ANDROID = "http://schemas.android.com/apk/res/android";
              
    private Bitmap rowBackground;
    private int rowBgHeight;
    private int columnNum;
              
    public RowGridView(Context context, AttributeSet attrs){
        super(context,attrs);
        //获得列数
        columnNum = attrs.getAttributeIntValue(NAMESPACE_ANDROID,"numColumns",3);
        //获取自定义属性
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.row_bg_grid_view);
        rowBgHeight = a.getDimensionPixelSize(R.styleable.row_bg_grid_view_row_bg_height,50);
        int row_bg_resource = a.getResourceId(R.styleable.row_bg_grid_view_row_background,-1);
        rowBackground = BitmapFactory.decodeResource(getResources(),row_bg_resource);
    }
              
    @Override
    protected void dispatchDraw(Canvas canvas){
        //行高
        int rHeight = getChildAt(0).getHeight();
        int width = getWidth();
        int rowNum = (int)Math.ceil(getChildCount()/(double)columnNum);
        //源绘制矩形
        Rect src = new Rect(0,0,rowBackground.getWidth(),rowBackground.getHeight());
        for(int i=0,y=rHeight-(rowBgHeight/2); i<rowNum; i++,y += rHeight){
            //目的绘制矩形
            Rect dst = new Rect(0,y,width,y+rowBgHeight);
            canvas.drawBitmap(rowBackground,src,dst,null);
        }
        super.dispatchDraw(canvas);
    }
}

上面第11行有这样一句

1
columnNum = attrs.getAttributeIntValue(NAMESPACE_ANDROID,"numColumns",3);

为什么不用getNumColumns?因为这个函数需要api level 11(即Android 3.0以上),而且有可能返回-1

两个自定义属性:背景和背景高度,在values/attrs.xml定义如下

1
2
3
4
5
6
7
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="row_bg_grid_view">
        <attr name="row_background" format="reference"/>
        <attr name="row_bg_height" format="dimension" />
    </declare-styleable>
</resources>

最后使用的方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">
    <com.best.oa.common.base.RowGridView
            android:id="@+id/functions"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dip"
            android:layout_weight="1"
            android:numColumns="2"
            oa:row_background="@drawable/grid_row_bg"
            oa:row_bg_height="60dip"
            android:stretchMode="columnWidth">
    </com.best.oa.common.base.RowGridView>
</LinearLayout>

为Android GridView 设置行背景的更多相关文章

  1. Android GridView设置行数

    普通的做法是设置一个高度,然后里面能显示出来几行就是几行,如果里面的内容高度变了,就需要重新调整高度来适配. 观察了一下它的onMeasure @Override protected void onM ...

  2. 雷林鹏分享:jQuery EasyUI 数据网格 - 条件设置行背景颜色

    jQuery EasyUI 数据网格 - 条件设置行背景颜色 本教程将向您展示如何根据一些条件改变数据网格(datagrid)组件的行样式.当 listprice 值大于 50 时,我们将为该行设置不 ...

  3. Android Studio设置图片背景及主题设置

    因为Android Studio是基于IDEA的,所以IDEA里面能用的插件Android Studio也能用,这次图片背景是依赖IDEA下的一个插件,名为BackgroundImage的插件,用户可 ...

  4. Android Studio设置行宽、格式化断行

    设置基于Android studio 1.2,其它版本可能位置不大一样,可以直接搜索 1.设置行宽 就是那条右标准线的位置:Setting-->Editor-->Code Style,右侧 ...

  5. Android GridView 指定行数,动态行宽,占满空间

    有时间我们需要 使用GridViw 让它占满父控件,例: 特别是在适配的时间比较麻烦,在不同的机型上可能分出下,下面空的太多,或有滚动条问题,; 下面说一下实现思路: 首先,设置GridView 为三 ...

  6. EasyUI 条件设置行背景颜色

    数据网格(datagrid)的 rowStyler 函数的设计目的是允许您自定义行样式. rowStyler 函数需要两个参数: rowIndex:行的索引,从 0 开始. rowData:该行相应的 ...

  7. Android 代码设置Activity 背景透明

    当指定Activity 样式 Them.Dialog 时候 又不同意用XML 设置 Activity 的背景颜色的时候 用代码 this.getWindow().getDecorView().setB ...

  8. jQuery EasyUI 数据网格 - 条件设置行背景颜色

    $('#tt').datagrid({ rowStyler:function(index,row){ if (row.listprice>50){ return 'color:blue;font ...

  9. Android 网络url设置View背景图

    imgstr为url网络图片地址,topllay是要设置背景的控件: 方法1.Android Glide设置View背景图 Glide.with(this).load(imgStr).asBitmap ...

随机推荐

  1. Safari浏览器的调试

    最近做浏览器兼容的时候发现Safari的脚本调试工具比较难找,因此与大家分享一下 1.找到脚本调试的选项 2.勾选此选项 3.在页面空白处右击打开调试窗口 4.看到下方的调试窗口了 细心的读者会发现, ...

  2. Oracle数据库常用函数

    Oracle常用函数: 20.COS返回一个给定数字的余弦SQL> select cos(-3.1415927) from dual;COS(-3.1415927)--------------- ...

  3. OC语言-01类和对象

    // cc 文件名.m -framework Foundation 编译链接 #import <Foundation/Foundation.h> //枚举性别 typedef enum{ ...

  4. 2016030205 - ubuntu安装mysql

    ubuntu上安装mysql 1.检查ubuntu上是否已经安装mysql sudo netstat -tap | grep mysql 本机上没有安装mysql 2.安装mysql服务器端和客户端 ...

  5. uoj164. 【清华集训2015】V 统计

    坑爹题面:http://uoj.ac/problem/164 正常题面: 对于一个序列支持下列5个操作: 1.区间加x 2.区间减x并与0取max 3.区间覆盖 4.单点查询 5.单点历史最大值查询 ...

  6. jQuery分页插件jBootstrapPage,一个Bootstrap风格的分页插件

    一个Bootstrap风格的分页控件,对于喜欢Bootstrap简洁美观和扁平化的同学可以关注jBootstrapPage, 目前jBootstrapPage最新版为V0.1,后续还有更多功能需要完善 ...

  7. HDU 3507 Print Article 斜率优化

    Print Article Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)To ...

  8. WordPress ‘get_allowed_mime_types’函数安全漏洞

    漏洞名称: WordPress ‘get_allowed_mime_types’函数安全漏洞 CNNVD编号: CNNVD-201309-170 发布时间: 2013-09-13 更新时间: 2013 ...

  9. Node.js权威指南 (1) - Node.js介绍

    1.1 Node.js概述 / 2 1.1.1 使用Node.js能够解决什么问题 / 2 1.1.2 实现高性能服务器 / 2 1.1.3 非阻塞型I/O及事件环机制 / 2 1.1.4 Node. ...

  10. Unity Twist Effect Black Hole

    Shader "Hidden/Twist Effect" {Properties { _MainTex ("Base (RGB)", 2D) = "w ...