在很多移动端或者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里的网格空隙的更多相关文章

  1. 注意Android里TextView控件的一个小坑,用android:theme来设置样式时动态载入的layout会丢失该样式

    注意Android里TextView控件的一个小坑,用android:theme来设置样式时动态载入的layout会丢失该样式 这个坑,必须要注意呀, 比如在用ListView的时候,如果在List_ ...

  2. Android里使用正則表達式

    在Android里怎样使用正則表達式: 以验证username为例.username一般字母开头,同意字母数字下划线.5-16个字节: String regEx = "^[a-zA-Z][a ...

  3. Android 自学之网格试图(GridView)和图片切换器(ImageSwitcher)功能和用法

    网格试图(GridView)用于在界面上按行,列分布的方式来显示多个组件. GridView和ListView有共同的父类:AbsListView,因此GridView和ListView具有一定的相似 ...

  4. android里R.layout.的问题

    今天,在Exlipse里的一个项目在.java文件里写  setContentView(R.layout.activity_problem);时,显示错误,以为是R.java文件里没有对应的activ ...

  5. android——学习:网格布局——GridLayout

    Android一开始就提供了几种布局控件,如线性布局LinearLayout.相对布局RelativeLayout和表格布局TableLayout等,但在很多情况下,这些布局控件是不能满足要求的,因此 ...

  6. Android布局_网格布局GirdLayout

    自Android4.0版本后新增的GirdLayout网格布局(API 14) <?xml version="1.0" encoding="utf-8"? ...

  7. Android里的多线程知识点

    1.Thread类与Runnable接口 子类继承Thread类实现跑自己逻辑的run方法,在调用Thread类的start方法后,会自动调用run方法,该对象只可以调用一次start方法,即Thre ...

  8. 【Android 界面效果34】Android里Service的bindService()和startService()混合使用深入分析

    .先讲讲怎么使用bindService()绑定服务 应用组件(客户端)可以调用bindService()绑定到一个service.Android系统之后调用service的onBind()方法,它返回 ...

  9. 在Android里完美实现基站和WIFI定位

    来自:http://www.cnblogs.com/coffeegg/archive/2011/10/01/2197129.html 众所周知的,在OPhone和大部分国产的Android定制机里不支 ...

随机推荐

  1. SimpliciTI协议地址分配

    1.多个ED节点和AP正确连接后,AP都会给ED分配一个相应的地址.当某个ED出现意外,比如电源问题,和AP断开连接,AP并不将该ED节点的地址消除.当该ED恢复正常,重新申请加入网络时,AP会检测该 ...

  2. GBK点阵显示字库的制作和使用

    转自:http://blog.csdn.net/exbob/article/details/6539643 GBK编码共收录汉字21003个.符号883个,并提供1894个造字码位,简.繁体字融于一库 ...

  3. 使用二次封装的openStack发行版本网卡至少有2个

  4. MySQL写入中文乱码

    这点确实很迷,我的数据库属性确实设置了utf-8字符集,但写入中文还是乱码,后来是直接修改了全局配置才修改过来. 1.进入MySQL的本地安装路径,我的安装路径是"C:\Program Fi ...

  5. 数组,for语句(补10.11)

    1.数组定义:一系列通数据类型的数据集合. 2.数组赋值的两种方法: 先定义后赋值:(赋值从0开始) var aa = new Arrey(); aa[0] = 1; aa[1] = 2; 定义并赋值 ...

  6. imgAreaSelect插件

    利用jquery的imgAreaSelect插件实现图片裁剪示例 将用户上传的图片进行裁剪再保存是现在web2.0应用中常常处理的工作,现在借助jquery的imgareaselect插件再配合PHP ...

  7. 当Python中混进一只薛定谔的猫……

    本文原创并首发于公众号[Python猫],未经授权,请勿转载. 原文地址:https://mp.weixin.qq.com/s/-fFVTgWVsydFsNu1nyxUzA Python 是一门强大的 ...

  8. 【转】PHP实现下载与压缩文件的封装与整理

    [转]PHP实现下载与压缩文件的封装与整理    https://mp.weixin.qq.com/s/BUI3QsdNi6Nqu0NhrUL8hQ 一.PHP实现打包zip并下载功能 $file_t ...

  9. 2014-8-5 NOIP(雾)模拟赛

    皇帝的烦恼(二分答案) Description 经过多年的杀戮,秦皇终于统一了中国.为了抵御外来的侵略,他准备在国土边境安置n名将军.不幸的是这n名将军羽翼渐丰,开始展露他们的狼子野心了.他们拒绝述职 ...

  10. js的Element.scrollIntoView的学习

    1.Element.scrollIntoView()    该方法让当前元素滚动到浏览器窗口的可是区域内: 2.语法: element.scrollIntoView();//等同于element.sc ...