android GridLayout 设置item间距方式
来源:互联网 发布:枪与玫瑰乐队知乎 编辑:程序博客网 时间:2024/06/11 13:40
完成一个自定义控件时突发奇想想用gridlayout,动态添加完item之后发现间距不好调整
网上回答类似问题的比较少,找了很久没找到;
Api没有明确描述,看了一会也没找到可调整间距的参数或方法;
我要完成的效果如下图:
于是开始测试,布局里面使用layout_margin
OK,生效(布局里面直接设置layout_margin是生效的,参考代码:
<GridLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:columnCount="6" android:orientation="horizontal" android:rowCount="5"> <Button android:id="@+id/btn01" android:layout_margin="20dp" /> <Button android:id="@+id/btn02" /> <Button android:id="@+id/btn03" /> </GridLayout>);
然后尝试在代码里面动态添加:
LinearLayout.LayoutParams ll = new LinearLayout.LayoutParams(txWidth, txHeight); ll.rightMargin = xOffset; ll.topMargin = yOffset / 2; ll.bottomMargin = yOffset / 2; tv.setLayoutParams(ll);奇怪的事情发生了:并不能生效!!
看了一下源码,豁然开朗:Gridlayout内部的组件不能直接设置LinearLayout.LayoutParams,
否则除了控件大小,其他一概不收,Margin自然也就不生效了;于是改正:
<pre name="code" class="java"> LinearLayout.LayoutParams ll = new LinearLayout.LayoutParams(txWidth, txHeight); GridLayout.LayoutParams gl = new GridLayout.LayoutParams(ll); gl.rightMargin = xOffset; gl.topMargin = yOffset / 2; gl.bottomMargin = yOffset / 2; tv.setLayoutParams(gl); addView(tv);
完美实现效果。
2 0
- android GridLayout 设置item间距方式
- android GridLayout 设置item间距
- Android RecyclerView设置item间距
- android 设置Gridlayout中item的位置
- Android设置Listview每个item的间距
- 为 GridLayout 的 RecyclerView 设置 item 间距,实现所有 Item 靠边对齐,中间留白的效果
- 设置ListView item间距
- RecycleView item 间距设置
- Android Listview item 间距
- android:设置listview里面每个item的上下间距
- Android中Recyclerview使用14----RecycleView的item间距设置
- RecycleView设置item上下间距
- beamer 中设置 item 之间的间距
- ListView item设置间距无效问题
- GridView 设置 item 间距的方法
- listview设置每个item的间距
- RecyclerView 设置item之间的间距
- listview设置item高度或间距
- string 类型的使用方法
- 高斯模糊相关链接
- Treap
- hive array、map、struct使用
- 图形学实验代码
- android GridLayout 设置item间距方式
- 浏览器缓存机制详细讲解
- Java分类统计次数,取max
- php str_pad()函数
- nutch1.9--windows下nutch基本操作
- 进度条 游戏中Win32进度条ProcessBar的编码
- js实现 页面显示系统时间(中文显示星期)、动态倒计时页面跳转
- Android学习之文件复制
- Android Volley框架的几种post提交请求方式