今天总结下Android开发中有关布局平分的相关技术和实现。

从一个简单的任务入手,“如何在水平方向上一左一右均匀地放置两个Button”,有很多种方式可以实现这个功能,在此做一个简单的总结,顺便深入理解下有关 gravity, layout_weight 等相关概念的原理和应用。

一、效果图

二、思考 RelativeLayout 和 LinearLayout 中分别如何左右放置button

(1) 在RelativeLayout中放置

由于 RelativeLayout 内部的子控件都可以指定相对位置,因此很容易实现左右放置两个Button的任务,如下所示:

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Left Button"

android:layout_alignParentLeft="true"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Right Button"

android:layout_alignParentRight="true"/>

效果如图所示:

(2) 在LinearLayout中放置

由于LinearLayout是顺序排放所有的控件,如果希望两个button分开地位于一左一右,则必须在两个Button之间再加一个layout_weight为1.0的隐形view才能实现。示例如下:

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_margin="10dp"

android:orientation="horizontal">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Left Button"/>

android:layout_width="0dp"

android:layout_weight="1.0"

android:layout_height="0dp"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Right Button"/>

效果如图所示:

三、 在水平方向“均分”整个Layout

其实,对于LinearLayout而言,最简单地平分方式就是将子控件的layout_width属性值设置为0,同时layout_weight属性设置为1.0这样,所有的子控件就会自动均分整个LinearLayout ,例如:

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_margin="10dp"

android:orientation="horizontal">

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1.0"

android:text="Left Button"/>

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1.0"

android:text="Right"/>

效果如图:

由图中效果可以看到,两个Button很好地一左一右平分了整个LinearLayout,但是由于Button的layout_width设置的是0dp,通过layout_weight来决定长度,因此Button都被横向拉长了,如果希望Button的layout_width设置为wrap_content,那该如何实现均分呢?其实,可以通过在Button外再包围一层LinearLayout来实现,示例如下:

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_margin="10dp"

android:orientation="horizontal">

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1.0"

android:orientation="horizontal">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Left Button"

android:layout_gravity="center"/>

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1.0"

android:orientation="horizontal"

android:gravity="center_horizontal">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Right Button"/>

这样地话,由两个Button外围的LinearLayout平分了整个水平的layout,两个button的大小采用wrap_content,也不会被拉伸,并且通过外围的LinearLayout的android:gravity属性实现了居中。

这里注意Button外围的LinearLayout的android:gravity属性,很关键,该属性决定其子控件在该控件中的位置。所以Button外围的LinearLayout设置了android:gravity="center",因此Button才能在该LinearLayout的正中央。

思考,如果给Button加一个android:gravity="right"的属性,会是什么效果呢?

答案: 会使得 Button 内部的文字向右靠齐,而不是在中央。

四、小结

关于布局的平分就聊到这里了,其实从这么一个简单的任务需求的实现,还是可以学到很多技巧和知识点的,文中有不清楚的或者错误的地方,欢迎留言讨论或者来信lujun.hust@gmail.com交流,,或者关注我的新浪微博 @卢_俊 获取最新的文章和资讯。

android平分布局的隐藏,Android开发实践:布局的平分相关推荐

  1. android平分布局的隐藏,Android布局 平分界面

    以前做iOS的,android水平属于渣渣以下,现在做界面都感觉是在吃糠,写一段查半天.心累到不想说话. 今天主要是遇到界面上三个空间并排排列,平分界面,其实就是和支付宝下面那些选项卡一样. 不讲中间 ...

  2. android view父级隐藏,Android指定的子级已经有父级。必须先对子级的父级调用removeView()...

    在我的应用中,我必须经常在两种布局之间切换.错误在下面发布的布局中发生. 第一次调用布局时,没有发生任何错误,一切都很好.然后,当我调用不同的布局(空白),然后再次调用我的布局时,出现以下错误: &g ...

  3. Android音视频点/直播模块开发实践总结-zz

    随着音视频领域的火热,在很多领域(教育,游戏,娱乐,体育,跑步,餐饮,音乐等)尝试做音视频直播/点播功能.那么作为开发一个小白,如何快速学习音视频基础知识,了解音视频编解码的传输协议,编解码方式,以及 ...

  4. 蓝牙android rssi测距,蓝牙RSSI测距开发实践记录

    补充些默认参数值,现在怀疑这款蓝牙可能不支持RSSI +NAME=BT05 +VERSION=Firmware V4.2.0,Bluetooth V4.0 LE +PIN=1234 +TYPE=0 + ...

  5. android监听键盘的隐藏,Android监听软键盘的显示和隐藏

    使用步骤 xml 布局文件布局,和普通的控件一下 获取SoftInputCanListenerEditText 实例,并设置监听器 Activity 注册的时候android:windowSoftIn ...

  6. android listview 滚动条不隐藏,Android ListView隐藏右侧滚动条功能

    关于ListView的滚动条几种情形: 1.默认情况:活动(滚动)时显示,不活动时隐藏. 2.活动和不活动时都显示. 3.活动和不活动时都隐藏. 上述集中情况,均有ListView的以下设置属性方法控 ...

  7. android 拨打电话但隐藏,android实现拨打电话但不弹出拨号界面

    这里只提供一个大概的思路,整个流程不是我一个人就能完成的.测试机型为htc one x,Android版本4.2.2,已ROOT. 要实现拨号程序可能很简单,一个简单的Intent就能实现,从发出意图 ...

  8. Android导航栏自动隐藏,Android隐藏和显示虚拟导航栏

    隐藏导航栏 /** * 隐藏虚拟按键,并且全屏 */ public static void hideBottomNav(Activity activity) { View decorView = ac ...

  9. unity导致android虚拟键,unity隐藏android机的虚拟按钮

    启动时 将下面有脚本挂载某个物件就行: using UnityEngine; using System.Collections; public class HideAndroidButtons : M ...

最新文章

  1. 通过url 下载文件
  2. SQL Server 2005 智能感知插件 - SQL Prompt 3.8.0.224
  3. 数据库-优化-数据库结构的优化-表范式化优化
  4. qt 16进制字符串和十六进制数_Python字符串类型及其操作
  5. (二)Mysql 基础了解,修改字符集,配置文件
  6. 现代软件工程 M1 博客要求
  7. android 焦点分发,Android TV 焦点分发原理解析
  8. 音频3A测试 NS降噪测试
  9. 棋盘格相机标定图片拍摄方法
  10. hp打印机一直显示正在打印中_HP打印机提示文档正在打印,但就是打印不了?...
  11. iOS 网络传输数据安全以及常用的加密算法使用
  12. 六则糟糕代码的优化方案分享
  13. 如何设置交易滑点?精确到tick 测算期货冲击成本(附源码)
  14. Visual Studio2010当前不会命中代码,源代码与原始版本不同问题的解决方法
  15. AT89S51单片机硬件结构
  16. HTML_06(Dom(03))
  17. 外卖点餐平台系统源码
  18. hadoop相关练习
  19. Leetcode-111 二叉树的最小深度(递归)
  20. 计网复习——数据链路层习题

热门文章

  1. raspberry pi(树莓派) + easycap d60 视频采集
  2. 金三银四之ConcurrentHashMap剖根问底栏目(二)
  3. 智力测试---20140731
  4. Vue之组件自定义事件的绑定和解绑
  5. 文件下载权限控制机制
  6. 我们都希望在最好的年华遇见一个人,可往往是遇见一个人才迎来最最好的年华
  7. 2022创业无货源电商,精细化运营选品蓝海类目,最新运营玩法解析
  8. layui清空表单数据_layui如何清除表单数据
  9. 深度分享阿里(蚂蚁金服)技术面试流程,附前期准备,学习方向
  10. html做的小游戏,用Html做一个“快乐鸟”小游戏