为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 ...
随机推荐
- [Linux]Ubuntu下如何将普通用户提升到root权限
转至:http://jingyan.baidu.com/album/6181c3e0780131152ef153ff.html?picindex=0&qq-pf-to=pcqq.c2c 在u ...
- Python环境搭建中解决C编译的问题
下载必要文件 Python Microsoft Visual C++ Compiler for Python 2.7 setuptools 安装Python 安装VCForPython27 在命令行下 ...
- Windows命令行下pip安装python whl包
因为做网页爬虫,需要用到一个爬新闻的BeautifulSoup 的包,然后再关网上下的是whl包,第一次装,虽然花了点时间,最后还是装上去了,记录一下,方便下次. 先发一下官方文档地址.http:// ...
- C# mvc 验证码2
public class ValidateCode { /// <summary> /// 產生圖形驗證碼. /// </su ...
- 学习Swift -- 构造器(中)
构造器(中) 值类型的构造器代理 构造器可以通过调用其它构造器来完成实例的部分构造过程.这一过程称为构造器代理,它能减少多个构造器间的代码重复. 构造器代理的实现规则和形式在值类型和类类型中有所不同. ...
- JSP 结构
网络服务器需要一个JSP引擎,也就是一个容器来处理JSP页面. 容器负责截获对JSP页面的请求.本教程使用内嵌JSP容器的Apache来支持JSP开发. JSP容器与Web服务器协同合作,为JSP的正 ...
- StartSSL免费SSL证书成功申请-HTTPS让访问网站更安全
StartSSL免费SSL证书成功申请-HTTPS让访问网站更安全 一.StartSSL个人证书登录申请 1.StartSSL官网: 1.官方首页:http://www.startssl.com/ 2 ...
- 【技术贴】解决支付宝充值信用卡还款跳转到网上银行报错Error 404 - Not Found
声明 : 本文在 GFDL 1.2 下发布,本文出处光大银行信用卡 http://bbs.090989.com/forum-186-1.html http://androidgao.blogspot ...
- HTML Imports
为什么需要导入? 先想想你在 web 上是如何加载不同类型的资源.对于 JS,我们有 <script src>.<link rel="stylesheet"> ...
- 关于Cookie跨域操作的一些总结
正常的cookie只能在一个应用中共享,即一个cookie只能由创建它的应用获得. 1.可在同一应用服务器内共享方法:设置cookie.setPath("/"); 本机to ...