Android里的网格空隙
在很多移动端或者web端开发中我们会遇到很多网格布局,如果我们使用线性布局来实现一些简单的网格布局就需要使用padding/margin等属性来使其对齐,代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="@dimen/spacing_medium">
<TextView
android:layout_width="match_parent"
android:layout_height="128dp"
android:background="@color/light_gray"
android:gravity="center"
android:textSize="@dimen/text_size"
android:text="@string/application_logo"
android:textAppearance="@android:style/TextAppearance.Material.Display1" />
<LinearLayout
android:id="@+id/buttons_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_first"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="@dimen/text_size"
android:background="@color/violet"
android:text="@string/button_1" />
<Button
android:id="@+id/btn_second"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="@dimen/text_size"
android:background="@color/blue"
android:text="@string/button_2" />
<Button
android:id="@+id/btn_third"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="@dimen/text_size"
android:background="@color/green"
android:text="@string/button_3" />
</LinearLayout>
</LinearLayout>
但是我们很多时候需要让这些网格元素之间有一些空隙,这样看起来好看一些,如下
这样看起来确实很漂亮,但是漂亮的同时问题来了,现在我们需要去掉BUTTON 3,去掉后就会发现最右边对齐出现了问题(因为我们是使用padding/margin来实现的),其实LinearLayout已经有间隙的概念,可以设置其子元素间的间隙,下面我们来定义我们自己的间隙,使得后面更加灵活的变化我们网格元素间的间隙。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size
android:width="@dimen/spacing_medium"
android:height="@dimen/spacing_medium" />
<solid android:color="@android:color/transparent" />
</shape>
现在我们可以给LinearLayout设置divider 和 showDividers (注意:android:divider, android:showDividers)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="@drawable/spacer_medium"
android:orientation="vertical"
android:padding="@dimen/spacing_medium"
android:showDividers="middle">
<!-- TextView -->
<LinearLayout
android:id="@+id/buttons_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="@drawable/spacer_medium"
android:orientation="horizontal"
android:showDividers="middle">
<!-- Buttons -->
</LinearLayout>
</LinearLayout>
这样我们可以自由方便的调节类似的网格布局之间的间隙了,这些技巧可以方便我们的布局,减少代码量,让我们的代码更加容易维护。
Android里的网格空隙的更多相关文章
- 注意Android里TextView控件的一个小坑,用android:theme来设置样式时动态载入的layout会丢失该样式
注意Android里TextView控件的一个小坑,用android:theme来设置样式时动态载入的layout会丢失该样式 这个坑,必须要注意呀, 比如在用ListView的时候,如果在List_ ...
- Android里使用正則表達式
在Android里怎样使用正則表達式: 以验证username为例.username一般字母开头,同意字母数字下划线.5-16个字节: String regEx = "^[a-zA-Z][a ...
- Android 自学之网格试图(GridView)和图片切换器(ImageSwitcher)功能和用法
网格试图(GridView)用于在界面上按行,列分布的方式来显示多个组件. GridView和ListView有共同的父类:AbsListView,因此GridView和ListView具有一定的相似 ...
- android里R.layout.的问题
今天,在Exlipse里的一个项目在.java文件里写 setContentView(R.layout.activity_problem);时,显示错误,以为是R.java文件里没有对应的activ ...
- android——学习:网格布局——GridLayout
Android一开始就提供了几种布局控件,如线性布局LinearLayout.相对布局RelativeLayout和表格布局TableLayout等,但在很多情况下,这些布局控件是不能满足要求的,因此 ...
- Android布局_网格布局GirdLayout
自Android4.0版本后新增的GirdLayout网格布局(API 14) <?xml version="1.0" encoding="utf-8"? ...
- Android里的多线程知识点
1.Thread类与Runnable接口 子类继承Thread类实现跑自己逻辑的run方法,在调用Thread类的start方法后,会自动调用run方法,该对象只可以调用一次start方法,即Thre ...
- 【Android 界面效果34】Android里Service的bindService()和startService()混合使用深入分析
.先讲讲怎么使用bindService()绑定服务 应用组件(客户端)可以调用bindService()绑定到一个service.Android系统之后调用service的onBind()方法,它返回 ...
- 在Android里完美实现基站和WIFI定位
来自:http://www.cnblogs.com/coffeegg/archive/2011/10/01/2197129.html 众所周知的,在OPhone和大部分国产的Android定制机里不支 ...
随机推荐
- μC/OS-II与RT-Thread对比—任务调度
在任务调度器的实现上,μC/OS-II和RT-Thread都采用了位图调度(bitmap scheduling),任务优先级的值越小则代表具有越高的优先级,主要区别在于实现形式,是采用多级队列的形式, ...
- AAAAAA
有可能被立案调查.暂停上市.退市风险警示*ST.特别处理ST的公司:银鸽投资(SH:600069).天山生物(SZ:300313).金贵银业(SZ:002716).美盛文化(SZ:002699).未名 ...
- 【238】◀▶IEW-Unit03
Unit 3 Media 柱状图 1.图片及model1分析 The graph below contains information about the average science test s ...
- 怎么解决sublime的插件自动被禁用
前两天,我的sublime text安装的很多插件都被自动禁用了,每次都要我自己重新启用一下才可以,后来从网上找到了解决方法. 找到“设置”-“Package Settings”-“Package C ...
- java之字符串转换
参考http://how2j.cn/k/number-string/number-string-parse/317.html 数字转字符串 方法1: 使用String类的静态方法valueOf 方法2 ...
- Oracle系统权限列表
当你新建一个用户,指定表空间之后,这个用户基本上什么都不能做,连接数据库都不可以.你要给这个用户赋各种权限. create session -----允许用户连接到数据 create tabl ...
- 转: Charles 从入门到精通
目录与版权 转载请保留顶部的 Charles 中国特惠内容,本文的内容主要包括: Charles 的简介 如何安装 Charles 将 Charles 设置成系统代理 Charles 主界面介绍 过滤 ...
- UVaLive 4254 Processor (二分+优先队列)
题意:有n个任务,每个任务有三个参数,r,d,w,表示该任务必须在[r,d]之间执行,工作量是w,处理器执行速度可以变化,当执行速度是s的时候, 一个工作量是w的任务需要需要的执行时间是w/s个工作单 ...
- 将Opencv java中的Mat通过jni传递到C++中的方法
public native void FindFeatures(long matAddrGr, long matAddrRgba); ... mRgba = new Mat(height, width ...
- 特性attribute,声明和使用attribute,应用attribute,AOP面向切面,多种方式实现AOP
1 特性attribute,和注释有什么区别2 声明和使用attribute3 应用attribute4 AOP面向切面5 多种方式实现AOP ---------------------------- ...