【Android】Layout 水平平分空间、垂直平分空间
来源:互联网 发布:freebsd linux nvidia 编辑:程序博客网 时间:2024/04/28 03:08
今天在论坛看到有人提问,如何让两个按钮平分一行空间。
大概效果如下:
有人说,设置宽度固定大小,这样应该可以达到效果(本人没试),但是如果使用不同分辨率的手机来看,肯定会有问题,影响用户体验。
也有人说,设置宽度属性为 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>
效果:
转:http://blog.csdn.net/feng88724/article/details/6217707
- 【Android】Layout 水平平分空间、垂直平分空间
- 【Android控件布局】Layout 水平平分空间、垂直平分空间
- 【Android】Layout 水平平分空间、垂直平分空间
- 【Android】Layout 水平平分空间、垂直平分空间
- 【Android控件布局】Layout 水平平分空间、垂直平分空间
- 【Android控件布局】Layout 水平平分空间、垂直平分空间
- Android 水平平分布局和垂直平分布局
- android 水平平分两个按钮
- Android 实现九宫格(GridView中各项平分空间)
- RelativeLayout布局下实现控件平分空间
- RelativeLayout 如何实现平分布局空间
- 初学安卓平分空间排列相同内嵌布局
- Android GridView水平平均分配(根据numColumns平分适配)
- android linearlayout 平分一行
- Android XML视图 平分效果
- android中多个LinearLayout平分布局
- 平分七筐鱼
- 平分蛋糕
- 【笔者支招:熟知Word应用10个小技巧】_7gyy
- JS新建一个页面,并让其居中
- UVa 10079 - Pizza Cutting
- QT 远程连接 SQL SERVER 注意事项
- vs2008\drivers\opengl2.h.c 模拟灰色320*320屏幕
- 【Android】Layout 水平平分空间、垂直平分空间
- 反射机制与系统耦合实例详解
- poj题目推荐+50题说明
- 一天时间必须记住的快捷键
- 网站原创文章为什么不被百度搜录的几种猜想
- Slony-I 2.1.0 同步postgreSQL
- 用shell命令操作oracle数据库
- N个数顺序进栈,出栈的情况
- linux操作命令日志 记录的方法