为Android GridView 设置行背景
经常有这样的需求,你的功能图标要像一个个物品,摆放在书架上,像这样:

我的思路比较简单,重载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 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 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 设置行背景的更多相关文章
- Android GridView设置行数
普通的做法是设置一个高度,然后里面能显示出来几行就是几行,如果里面的内容高度变了,就需要重新调整高度来适配. 观察了一下它的onMeasure @Override protected void onM ...
- 雷林鹏分享:jQuery EasyUI 数据网格 - 条件设置行背景颜色
jQuery EasyUI 数据网格 - 条件设置行背景颜色 本教程将向您展示如何根据一些条件改变数据网格(datagrid)组件的行样式.当 listprice 值大于 50 时,我们将为该行设置不 ...
- Android Studio设置图片背景及主题设置
因为Android Studio是基于IDEA的,所以IDEA里面能用的插件Android Studio也能用,这次图片背景是依赖IDEA下的一个插件,名为BackgroundImage的插件,用户可 ...
- Android Studio设置行宽、格式化断行
设置基于Android studio 1.2,其它版本可能位置不大一样,可以直接搜索 1.设置行宽 就是那条右标准线的位置:Setting-->Editor-->Code Style,右侧 ...
- Android GridView 指定行数,动态行宽,占满空间
有时间我们需要 使用GridViw 让它占满父控件,例: 特别是在适配的时间比较麻烦,在不同的机型上可能分出下,下面空的太多,或有滚动条问题,; 下面说一下实现思路: 首先,设置GridView 为三 ...
- EasyUI 条件设置行背景颜色
数据网格(datagrid)的 rowStyler 函数的设计目的是允许您自定义行样式. rowStyler 函数需要两个参数: rowIndex:行的索引,从 0 开始. rowData:该行相应的 ...
- Android 代码设置Activity 背景透明
当指定Activity 样式 Them.Dialog 时候 又不同意用XML 设置 Activity 的背景颜色的时候 用代码 this.getWindow().getDecorView().setB ...
- jQuery EasyUI 数据网格 - 条件设置行背景颜色
$('#tt').datagrid({ rowStyler:function(index,row){ if (row.listprice>50){ return 'color:blue;font ...
- Android 网络url设置View背景图
imgstr为url网络图片地址,topllay是要设置背景的控件: 方法1.Android Glide设置View背景图 Glide.with(this).load(imgStr).asBitmap ...
随机推荐
- Python 统计代码行
正在学习 Python, 做了个统计代码行的功能, 参考了网上很多前辈的帖子,添加了感觉还是比较实用的功能, 只是windows下测试了,而且代码文件编码形式是 utf-8的. 如果使用其它编码形式的 ...
- jQuery 侧栏菜单点击body消失
其实就在弹出菜单时 让菜单外部有个全屏大小的遮罩层
- 帝国cms在任意位置调用指定id的栏目名称和链接
注意,这个代码无须放在灵动标签中,直接写入模板相应的位置就行了.[1]调用栏目名称: <?=$class_r[栏目ID]['classname']?> 示例:<?=$class_ ...
- Activity singleTop启动模式
栈顶单例模式 和standard模式一样, 只有Activity已经存在并且位于栈顶时, 不会重新创建. 其他时候都会创建新的Activity,然后放在栈顶
- 编程思想—面向切面编程(AOP)
谈到面向切面的编程,我们很容易关联到面向对象编程(OOP).个人对这两种编程方式的解释为:两种编程思想只是站在编程的角度问题. OOP注重的是对象,怎么对对象行为和方法的抽象.如何封装一个具有完整属性 ...
- Meta 的两个 相关属性
Meta标签中的apple-mobile-web-app-status-bar-style属性及含义: “apple-mobile-web-app-status-bar-style”作用是控制状态栏显 ...
- Contest 20140923 登月计划 BabyStepGaintStep
登月计划 查看 提交 统计 提问 总时间限制: 40000ms 内存限制: 256000kB 描述 HJA在和学弟学数学,于是便有了一道非常简单的数学题:求满足 的最小自然数x. 输入 输入数据一 ...
- [BZOJ 1058] [ZJOI2007] 报表统计 【平衡树】
题目链接:BZOJ - 1058 题目分析 这道题看似是需要在序列中插入一些数字,但其实询问的内容只与相邻的元素有关. 那么我们只要对每个位置维护两个数 Ai, Bi, Ai 就是初始序列中 i 这个 ...
- 5种php加密工具zendGuard、ionCube、SourceCop、SourceGuardian、phpShield
PHP做桌面应用的想法: 除去icudt55.dll,PHP7用7ZIP压缩后不足7MB,而PHP自带了SQLite和CLI HTTP Server,用户打开浏览器就能访问PHP开发的桌面应用.如果源 ...
- SysErrorMessage 函数和系统错误信息表
在看 API 文档时, 我们经常见到 GetLastError; 它可以返回操作后系统给的提示.但 GetLastError 返回的只是一个信息代码, 如何返回对应的具体信息呢?FormatMessa ...