为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 ...
随机推荐
- java 整体字体样式设置
两种方式: 1.UIManager.put("Button.font", new Font("MS UI Gothic", Font.PLAIN, 24)) ...
- adodb配置与使用
=========================================php100:80:ADODB PHP数据库万能引擎类 ADODB PHP数据库介绍与特点 ADODB 是一种兼容的各 ...
- ecshop 报错
ECShop出现Strict Standards: Only variables should be passed b (2014-06-04 17:00:37) 转载▼ 标签: ecshop 报错 ...
- 【C语言】重定向和文件
重定向和文件 一.相关基础知识 重定向:在计算机领域,重定向是大多数命令行解释器所具有的功能,包括各种可以将标准流重定向用户规定地点的Unix shells. 输入重定向:可以使程序能够使用文件代替键 ...
- button 垂直分布
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];//button的类型 button.frame = CGRectMak ...
- em(倍)与px的区别(转载)
在国内网站中,包括三大门户,以及"引领"中国网站设计潮流的蓝色理想,ChinaUI等都是使用了px作为字体单位.只有百度好歹做了个可调的表率.而 在大洋彼岸,几乎所有的主流站点都使 ...
- BZOJ4033 T1
Description 有一棵点数为\(N\)的树,树边有边权.给你一个在\(0-N\)之内的正整数\(K\),你要在这棵树中选择\(K\)个点,将其染成黑色,并将其他的\(N-K\)个点染成白色.将 ...
- 从1500万用户巅峰跌落的app,血泪回顾图片社交那些坑
饭桌君说 第八届小饭桌创业课堂来了一位特殊的分享嘉宾,他曾经参与了一款当时极具风头的图片社交app的创始团队,靠谱团队,用户量急速上升到1500万,公司获得A轮……一切看上去都那么美好. 可是,由于各 ...
- [topcoder]BinarySearchable
http://community.topcoder.com/stat?c=problem_statement&pm=5869&rd=8078 这道题有点意思,思考理解后,就是找数组中的 ...
- android Process.killProcess 和 System.exit(0) 区别
1 Process.killProcess 和 System.exit(0) 两个都会 kill 掉当前进程. 你可以打开 DDMS 查看进程号,或 adb shell 进入 shell 然后 ps ...