【Android控件布局】Layout 水平平分空间、垂直平分空间
今天在论坛看到有人提问,如何让两个按钮平分一行空间。
大概效果如下:
有人说,设置宽度固定大小,这样应该可以达到效果(本人没试),但是如果使用不同分辨率的手机来看,肯定会有问题,影响用户体验。
也有人说,设置宽度属性为 fill_parent, 其实这样是不行的。 页面上只会显示一个按钮,要么A ,要么B。(看你布局文件而定)
那么有什么解决办法呢?
本人试了RelativeLayout、TableLayout,发现都不可以~~ 最后在Android实例中发现, LinearLayout是可以的。
把上图效果的布局贴出来给大家参考一下吧: (会的就不用看了,虽然简单,但是想不到的话花一天也未必能做的出来)
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <Button
- android:id="@+id/bt1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="A"
- android:layout_weight="1"/>
- <Button android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="B"
- android:layout_weight="1"/>
- </LinearLayout>
其中主要就是 android:layout_weight="1", 只要让两个按钮的权重相同就可以,值无所谓。
下面贴一下Android 带的布局例子,水平平分、垂直平分都有:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <LinearLayout android:orientation="horizontal"
- android:layout_width="fill_parent" android:layout_height="fill_parent"
- android:layout_weight="1">
- <TextView android:text="red" android:gravity="center_horizontal"
- android:background="#aa0000" android:layout_width="wrap_content"
- android:layout_height="fill_parent" android:layout_weight="1" />
- <TextView android:text="green" android:gravity="center_horizontal"
- android:background="#00aa00" android:layout_width="wrap_content"
- android:layout_height="fill_parent" android:layout_weight="1" />
- <TextView android:text="blue" android:gravity="center_horizontal"
- android:background="#0000aa" android:layout_width="wrap_content"
- android:layout_height="fill_parent" android:layout_weight="1" />
- <TextView android:text="yellow" android:gravity="center_horizontal"
- android:background="#aaaa00" android:layout_width="wrap_content"
- android:layout_height="fill_parent" android:layout_weight="1" />
- </LinearLayout>
- <LinearLayout android:orientation="vertical"
- android:layout_width="fill_parent" android:layout_height="fill_parent"
- android:layout_weight="1">
- <TextView android:text="row one" android:textSize="15pt"
- android:layout_width="fill_parent" android:layout_height="wrap_content"
- android:layout_weight="1" />
- <TextView android:text="row two" android:textSize="15pt"
- android:layout_width="fill_parent" android:layout_height="wrap_content"
- android:layout_weight="1" />
- <TextView android:text="row three" android:textSize="15pt"
- android:layout_width="fill_parent" android:layout_height="wrap_content"
- android:layout_weight="1" />
- <TextView android:text="row four" android:textSize="15pt"
- android:layout_width="fill_parent" android:layout_height="wrap_content"
- android:layout_weight="1" />
- </LinearLayout>
- </LinearLayout>
效果:
【Android控件布局】Layout 水平平分空间、垂直平分空间相关推荐
- android 控件布局(顶部、底部......) 留着自己用
android 控件布局(顶部.底部......) 留着自己用 android:layout_above 将该控件的底部至于给定ID的控件之上 android:layout_below 将该控件的顶部 ...
- Android控件布局属性全解(一看就会)
Android有太多的布局属性繁琐又晦涩,下面结合了网上很多资料汇总. 内容太多,可以Ctrl+F进行搜索 第一类:属性值为true或false android:layout_centerHrizon ...
- Android 控件布局常用属性
<!--单个控件经常用到 android:id -- 为控件指定相应的ID android:text -- 指定控件当中显示的文字,需要注意的是,这里尽量使用strings.xml文件当中的字符 ...
- android控件在同一水平,android – 在linearlayout中水平对齐按钮,间距相等
我试图制作一个线性布局,水平放置5个按钮,间距相等,但所有按钮的大小(宽度)应仅为40dp. 我试过这个: android:background="#DCE1DC" android ...
- Android笔记之Android基本控件布局与Activity的生命周期
前言 喜欢安卓里千奇百怪的app,酷炫的UI或者好玩的游戏.想知道那些软件是如何做的,为什么我在微博里发一条消息,就能一直存在网上,让别人看到?为什么我点击一个按钮就出现了一个效果,某个功能?为什么滑 ...
- android比较常用的布局,Android_九宫图比较常用的多控件布局(GridView)使用介绍,GridView跟ListView都是比较常用的 - phpStudy...
九宫图比较常用的多控件布局(GridView)使用介绍 GridView跟ListView都是比较常用的多控件布局,而GridView更是实现九宫图的首选!本文就是介绍如何使用GridView实现九宫 ...
- 【Android 初学】3、控件布局初步
Start Android 什么是控件布局 所谓的控件布局方法,就是指控制控件在Activity当中的位置.大小.颜色以及其他控件样式属性的方法. 控件的布局,在android中,有两种方法: 1.使 ...
- android上传文件用哪个布局,每周总结20130821——android控件的尺寸、http文件上传...
Android控件的尺寸 android开发中,可以通过编写XML格式的布局文件来实现布局,也可以用纯代码进行布局,通常都是选择XML文件布局.在XML布局文件中,与控件的尺寸有关的属性有androi ...
- android控件属性
android控件属性 控件属性: android属性 Android功能强大,界面华丽,但是众多的布局属性就害苦了开发者,下面这篇文章结合了网上不少资料, 第一类:属性值为true或false an ...
最新文章
- 经验 | Pytorch还是Tensorflow?英伟达工程师帮你总结了
- 别再写 main 方法测试了,太 Low!这才是专业 Java 测试方法!
- 洛谷 P1865 A % B Problem[筛素数/前缀和思想/区间质数个数]
- 基于struts2+hibernate+spring(ssh2)的登录验证码的实现
- 独家直播!阿里移动前端开源框架Weex揭秘
- 10个业界最流行的Kubernetes发行版
- MySql关键字-保留字
- hive与hbase数据类型对应关系
- javax.servlet.http.HttpServletResponse接口(HTTP版本)
- T - SQL(常用语句)
- oracle查看归档日志是否开启,Oracle 开启归档日志以及关闭归档日志
- FileUpload文件上传控件
- 计算机系统常见故障及处理,电脑常见故障以及解决方案都在这里!
- linux 网卡队列深度,linux 磁盘队列深度nr_requests 和 queue_depth
- Manjaro安装与软件硬件基本配置(保姆级)
- “二进制负数 补码右移和原码算术右移 产生的真值偏差”分析
- error: exportArchive: No signing certificate “iOS Distribution“ found
- C语言高级应用---操作linux下V4L2摄像头应用程序
- 计算机 显卡 淘汰,早该淘汰的VGA模拟接口:新显卡不再支持
- 使用nvm安装node报错,Could not retrieve https://nodejs.org/dist/latest/SHASUMS256.txt. Get “https://nodej