如上图,要想实现3个button线性排列并且使它们的大小相同、间隔相等、而且整体填充满整个linearlayout,我们一般的做法是在每两个button之间放一个固定宽度的view,然后设置button的宽度为0、layout_weight为1。这样虽能实现功能,可是总感觉不方便,特别是button多的时候。

今天介绍另一种简单、优雅的方法,就是利用android:divider属性。

1、首先新建一个固有的width/height的Drawable:

spacer_medium.xml

<?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>

2.然后设置LinearLayout的android:divider="@drawable/spacer_medium",并设置android:showDividers="middle",这样就完美解决了linearLayout的元素之间的间隔问题。

<LinearLayout
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:divider="@drawable/spacer_medium"
android:background="#77000000"
android:showDividers="middle">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="button"/> <Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="button"/> <Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="button"/> </LinearLayout>

参考:http://blog.csdn.net/startupmount/article/details/41745715

巧用android:divider属性设置LinearLayout中元素之间的间隔的更多相关文章

  1. 一起学android之怎样设置TextView中不同字段的字体颜色(22)

    在这里先看看效果图: OK,有时候,在我们的项目中会要求TextView中文本有一部分的字体颜色不一样.这时我们应该使用 SpannableStringBuilder这个工具类,当然这个类的功能非常强 ...

  2. 利用Jquery获取、设置iframe中元素

    <iframe id="iframe" src="'+url+'"></iframe>'; //iframe加载完成后 $(" ...

  3. 【Android布局】在程序中设置android:gravity 和 android:layout_Gravity属性

    在进行UI布局的时候,可能常常会用到 android:gravity  和 android:layout_Gravity 这两个属性. 关于这两个属性的差别,网上已经有许多人进行了说明,这边再简单说一 ...

  4. ZT Android布局】在程序中设置android:gravity 和 android:layout_Gravity属性

    Android布局]在程序中设置android:gravity 和 android:layout_Gravity属性 分类: [Android基础] 2011-04-19 16:06 54739人阅读 ...

  5. 【Android布局】在程序中设置android:gravity 和 android:layout_Gravity属性——位置设置偏向

    LinearLayout有两个非常相似的属性: android:gravity与android:layout_gravity. 他们的区别在于: android:gravity 属性是对该view中内 ...

  6. 【转】在程序中设置android:gravity 和 android:layout_Gravity属性

    在进行UI布局的时候,可能经常会用到 android:gravity  和 android:layout_Gravity 这两个属性. 关于这两个属性的区别,网上已经有很多人进行了说明,这边再简单说一 ...

  7. 我的Android进阶之旅------&gt;怎样在多个LinearLayout中加入分隔线

    假设要适合于全部的Android版本号,能够在多个LinearLayout放置用于显示分隔线的View. 比如,放一个ImageView组件.然后将其背景设为分隔线的颜色或图像,分隔线View的定义代 ...

  8. 我的Android进阶之旅------>如何在多个LinearLayout中添加分隔线

    如果要适合于所有的Android版本,可以在多个LinearLayout放置用于显示分隔线的View.例如,放一个ImageView组件,然后将其背景设为分隔线的颜色或图像,分隔线View的定义代码如 ...

  9. Android 布局学习之——LinearLayout的layout_weight属性

    一直对layout_weight属性感到比较困惑,今天学习一下,来深入了解layout_weight属性和它的用法.     定义     首先,看看Android官方文档是怎么说的,毕竟人家才是权威 ...

随机推荐

  1. SpringCloud广告系统随想

    广告系统需要两个方面:一个广告投放系统,一个是广告检索系统. 两个系统针对的人群不同: 1.广告投放系统针对的是广告主,他们出资进行广告的投放,将广告的一些数据投放到系统中. 2.广告检索系统针对的是 ...

  2. frp 配置多个 web 项目,无需购买域名 (访问内网可视化界面,jupyter noterbook, visdom, tensorboard)

    frp 配置多个 web 项目,无需购买域名 简单配置,参考 前言: 网上也有很多教程包括官方文档,都需要购买域名,并且把 frpc.ini 中 [web]节配置的  custom_domains 的 ...

  3. vue实现curd功能

    一.实现效果 二.实现 (一)实现增加用户功能 Vuserlist组件中 <template> <div class="panel panel-default"& ...

  4. leetcode-158周赛-5222-分割字符串

    题目描述: 自己的提交: class Solution: def balancedStringSplit(self, s: str) -> int: if not s:return 0 res ...

  5. windows启动redis失败

    # Warning: no config file specified, using the default config. In order to specify a config file use ...

  6. NX二次开发-打开弹出当前part所在的文件夹

    #include <uf.h> #include <uf_part.h> #include <atlstr.h> #include <iostream> ...

  7. Mysql 触发器写法

    DELIMITER $$ USE `库名`$$ DROP TRIGGER /*!50032 IF EXISTS */ `trig_contract_status`$$ CREATE /*!50017 ...

  8. jvm-多线程

    多线程的目的 为什么要使用多线程?可以简单的分两个方面来说: 在多个cpu核心下,多线程的好处是显而易见的,不然多个cpu核心只跑一个线程其他的核心就都浪费了: 即便不考虑多核心,在单核下,多线程也是 ...

  9. 关于JQuery Ajax 跨域 访问.net WebService

    关于这个 jQuery Ajax跨域访问 WebService 前天整了好几个小时没整明白 今天再看一下 结果突然就顿悟了 1.建一个空webApplication --添加--新建项--web服务( ...

  10. (一)hello word

    1.在如图demo文件夹下可以编写自己的后端代码. 我们编写hello的接口代码如下: @RestController @RequestMapping("/hello") @Slf ...