今天在论坛看到有人提问,如何让两个按钮平分一行空间。

大概效果如下:

有人说,设置宽度固定大小,这样应该可以达到效果(本人没试),但是如果使用不同分辨率的手机来看,肯定会有问题,影响用户体验。

也有人说,设置宽度属性为 fill_parent, 其实这样是不行的。 页面上只会显示一个按钮,要么A ,要么B。(看你布局文件而定)

那么有什么解决办法呢?

本人试了RelativeLayout、TableLayout,发现都不可以~~ 最后在Android实例中发现, LinearLayout是可以的。

把上图效果的布局贴出来给大家参考一下吧: (会的就不用看了,虽然简单,但是想不到的话花一天也未必能做的出来)

[xhtml]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="horizontal"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <Button
  8. android:id="@+id/bt1"
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. android:text="A"
  12. android:layout_weight="1"/>
  13. <Button android:layout_width="fill_parent"
  14. android:layout_height="wrap_content"
  15. android:text="B"
  16. android:layout_weight="1"/>
  17. </LinearLayout>

其中主要就是 android:layout_weight="1",  只要让两个按钮的权重相同就可以,值无所谓。

下面贴一下Android 带的布局例子,水平平分、垂直平分都有:

[xhtml]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical" android:layout_width="fill_parent"
  4. android:layout_height="fill_parent">
  5. <LinearLayout android:orientation="horizontal"
  6. android:layout_width="fill_parent" android:layout_height="fill_parent"
  7. android:layout_weight="1">
  8. <TextView android:text="red" android:gravity="center_horizontal"
  9. android:background="#aa0000" android:layout_width="wrap_content"
  10. android:layout_height="fill_parent" android:layout_weight="1" />
  11. <TextView android:text="green" android:gravity="center_horizontal"
  12. android:background="#00aa00" android:layout_width="wrap_content"
  13. android:layout_height="fill_parent" android:layout_weight="1" />
  14. <TextView android:text="blue" android:gravity="center_horizontal"
  15. android:background="#0000aa" android:layout_width="wrap_content"
  16. android:layout_height="fill_parent" android:layout_weight="1" />
  17. <TextView android:text="yellow" android:gravity="center_horizontal"
  18. android:background="#aaaa00" android:layout_width="wrap_content"
  19. android:layout_height="fill_parent" android:layout_weight="1" />
  20. </LinearLayout>
  21. <LinearLayout android:orientation="vertical"
  22. android:layout_width="fill_parent" android:layout_height="fill_parent"
  23. android:layout_weight="1">
  24. <TextView android:text="row one" android:textSize="15pt"
  25. android:layout_width="fill_parent" android:layout_height="wrap_content"
  26. android:layout_weight="1" />
  27. <TextView android:text="row two" android:textSize="15pt"
  28. android:layout_width="fill_parent" android:layout_height="wrap_content"
  29. android:layout_weight="1" />
  30. <TextView android:text="row three" android:textSize="15pt"
  31. android:layout_width="fill_parent" android:layout_height="wrap_content"
  32. android:layout_weight="1" />
  33. <TextView android:text="row four" android:textSize="15pt"
  34. android:layout_width="fill_parent" android:layout_height="wrap_content"
  35. android:layout_weight="1" />
  36. </LinearLayout>
  37. </LinearLayout>

效果:

【Android控件布局】Layout 水平平分空间、垂直平分空间相关推荐

  1. android 控件布局(顶部、底部......) 留着自己用

    android 控件布局(顶部.底部......) 留着自己用 android:layout_above 将该控件的底部至于给定ID的控件之上 android:layout_below 将该控件的顶部 ...

  2. Android控件布局属性全解(一看就会)

    Android有太多的布局属性繁琐又晦涩,下面结合了网上很多资料汇总. 内容太多,可以Ctrl+F进行搜索 第一类:属性值为true或false android:layout_centerHrizon ...

  3. Android 控件布局常用属性

    <!--单个控件经常用到 android:id -- 为控件指定相应的ID android:text -- 指定控件当中显示的文字,需要注意的是,这里尽量使用strings.xml文件当中的字符 ...

  4. android控件在同一水平,android – 在linearlayout中水平对齐按钮,间距相等

    我试图制作一个线性布局,水平放置5个按钮,间距相等,但所有按钮的大小(宽度)应仅为40dp. 我试过这个: android:background="#DCE1DC" android ...

  5. Android笔记之Android基本控件布局与Activity的生命周期

    前言 喜欢安卓里千奇百怪的app,酷炫的UI或者好玩的游戏.想知道那些软件是如何做的,为什么我在微博里发一条消息,就能一直存在网上,让别人看到?为什么我点击一个按钮就出现了一个效果,某个功能?为什么滑 ...

  6. android比较常用的布局,Android_九宫图比较常用的多控件布局(GridView)使用介绍,GridView跟ListView都是比较常用的 - phpStudy...

    九宫图比较常用的多控件布局(GridView)使用介绍 GridView跟ListView都是比较常用的多控件布局,而GridView更是实现九宫图的首选!本文就是介绍如何使用GridView实现九宫 ...

  7. 【Android 初学】3、控件布局初步

    Start Android 什么是控件布局 所谓的控件布局方法,就是指控制控件在Activity当中的位置.大小.颜色以及其他控件样式属性的方法. 控件的布局,在android中,有两种方法: 1.使 ...

  8. android上传文件用哪个布局,每周总结20130821——android控件的尺寸、http文件上传...

    Android控件的尺寸 android开发中,可以通过编写XML格式的布局文件来实现布局,也可以用纯代码进行布局,通常都是选择XML文件布局.在XML布局文件中,与控件的尺寸有关的属性有androi ...

  9. android控件属性

    android控件属性 控件属性: android属性 Android功能强大,界面华丽,但是众多的布局属性就害苦了开发者,下面这篇文章结合了网上不少资料, 第一类:属性值为true或false an ...

最新文章

  1. 经验 | Pytorch还是Tensorflow?英伟达工程师帮你总结了
  2. 别再写 main 方法测试了,太 Low!这才是专业 Java 测试方法!
  3. 洛谷 P1865 A % B Problem[筛素数/前缀和思想/区间质数个数]
  4. 基于struts2+hibernate+spring(ssh2)的登录验证码的实现
  5. 独家直播!阿里移动前端开源框架Weex揭秘
  6. 10个业界最流行的Kubernetes发行版
  7. MySql关键字-保留字
  8. hive与hbase数据类型对应关系
  9. javax.servlet.http.HttpServletResponse接口(HTTP版本)
  10. T - SQL(常用语句)
  11. oracle查看归档日志是否开启,Oracle 开启归档日志以及关闭归档日志
  12. FileUpload文件上传控件
  13. 计算机系统常见故障及处理,电脑常见故障以及解决方案都在这里!
  14. linux 网卡队列深度,linux 磁盘队列深度nr_requests 和 queue_depth
  15. Manjaro安装与软件硬件基本配置(保姆级)
  16. “二进制负数 补码右移和原码算术右移 产生的真值偏差”分析
  17. error: exportArchive: No signing certificate “iOS Distribution“ found
  18. C语言高级应用---操作linux下V4L2摄像头应用程序
  19. 计算机 显卡 淘汰,早该淘汰的VGA模拟接口:新显卡不再支持
  20. 使用nvm安装node报错,Could not retrieve https://nodejs.org/dist/latest/SHASUMS256.txt. Get “https://nodej

热门文章

  1. java高级程序员该何去何从
  2. 【Java】面向对象继承法举例3——猫狗动物类
  3. 微信小程序-芒果时钟
  4. 相关性分析的算法类别
  5. 南京邮电大学电工电子基础B实验四(戴维南与诺顿定理)
  6. 大数据系列——Hadoop理论
  7. 输入框判断昵称是否合法
  8. 面向Java程序员的15个SpringBoot面试问题和答案
  9. 如果收到程序员的爱情表白书……
  10. 一.投资基本面分析(微观宏观经济)