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定制机里不支 ...
随机推荐
- VijosP1595:学校网络(有向图变强连通图)
描述 一些学校的校园网连接在一个计算机网络上.学校之间存在软件支援协议.每个学校都有它应支援的学校名单(学校a支援学校b,并不表示学校b一定支援学校a).当某校获得一个新软件时,无论是直接得到的还是从 ...
- ScrollView cannot scroll in Slidinguppanellayout 解决办法
xml源码如下 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:an ...
- AngularJs(Part 3)--注册服务
有以下5中方法注册一个AngularJS可以识别的Service value和constant是两个极其简单的方法,只有少数情况下会使用. service已经开始复杂了起来.而factory是我认为既 ...
- 使用Spring的jdbcTemplate进一步简…
先看applicationContext.xml配置文件: 版权声明:本文为博主原创文章,未经博主允许不得转载.
- Uncommon Words from Two Sentences
https://leetcode.com/problems/uncommon-words-from-two-sentences We are given two sentences A and B. ...
- HashMap为什么是线程不安全的
HashMap底层是一个Entry数组,当发生hash冲突的时候,hashmap是采用链表的方式来解决的,在对应的数组位置存放链表的头结点.对链表而言,新加入的节点会从头结点加入. 我们来分析一下多线 ...
- 漫画:深入浅出 ES 模块
本文来自网易云社区. 本文翻译自:ES modules: A cartoon deep-dive ES 模块为 JavaScript 提供了官方标准化的模块系统.然而,这中间经历了一些时间 —— 近 ...
- lightoj1009【DFS】
思路: 连通快+二分图,每次+二分图大的元素个数. #include<bits/stdc++.h> using namespace std; typedef unsigned long l ...
- Docker安装nginx以及负载均衡
首先在linux系统中新建一个data文件夹进行nginx容器的创建--即为:mkdir data. 一:第一次 1 第一步: 使用 docker pull nginx将nginx的镜像从仓库下载下来 ...
- RabbitMQ下载安装教程 Windows10
https://blog.csdn.net/weixin_39735923/article/details/79288578