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

我的思路比较简单,重载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. 结合rpyc使用python实现动态升级的方法

    动态升级,就是程序不退出的情况下,将其代码更新的策略.假设集群含有多个机器,然后每个机器部署一套程序,当升级的时候就要去所有的上面部署一把. (1)有个包装程序专门负责接口并检查是否需要更新,当需要更 ...

  2. Django1.7.1设置TEMPLATE_DIRS

    首先附上我的django工程目录结构: mysite│ db.sqlite3│ manage.py│├─mysite   │ settings.py   │ urls.py   │ views.py  ...

  3. activity学习(1) 生命周期理解

    可以忽略onWindowFocusChanged.onSaveInstanceState.onRestoreInstanceState几个事件,这几个事件官网中的生命周期里面没有提到.忽略掉这几个方法 ...

  4. hadoop 异常处理实例(一)hadoop内存配置项

    Exception in thread "main" java.io.IOException: Job failed! at org.apache.hadoop.mapred.Jo ...

  5. [BZOJ 1500] [NOI2005] 维修数列

    题目链接:BZOJ - 1500 题目分析 我要先说一下,这道题我写了一晚上,然后Debug了一整个白天..........再一次被自己的蒟蒻程度震惊= = 这道题是传说中的Splay维护数列的Bos ...

  6. 【Java】Java里String 的equals和==

    Java里面有对象和对象的引用的概念,在String方面,==比较的是引用,equals比较的是对象的具体值. String s1 = new String("abc");Stri ...

  7. 修炼debug

    常用方法: alert console.log 行号手工打breakpoints 手工加入debugger:配合条件if(){debugger;} break on dom modify eventL ...

  8. UIImageView之我的动画为什么停了?UIImageView, highLighted,animationImages

    如果你的动画总是停了!停了!停了!不管你想不想都停,这里有个参考,你可以看看!这只是一种可能性!!! 受最近看到段子影响,画风略诡异,不喜勿喷. 最近在“刻”动画!!! 为什么是“刻”,动画写了3周啊 ...

  9. python包管理-distutils,setuptools,pip,virtualenv等介绍

    python包管理-distutils,setuptools,pip,virtualenv等介绍 对于每个编程语言来说打包和发布开发包往往非常重要,而作为一个编程者能够快速容易的获得并应用这些由第三方 ...

  10. QT5.1.1中MinGW4.8的环境变量配置

    1.右击“我的电脑”图标,在弹出的菜单上选择“属性(R)”菜单项. 2.选择“高级”选项卡.点击“环境变量”按钮. 3.点击“新建(W)”按钮,新建环境变量:MINGW_HOME,变量值为MinGW的 ...