【Android】Layout 水平平分空间、垂直平分空间
来源:互联网 发布:js验证 是否阅读协议 编辑:程序博客网 时间:2024/04/29 02:06
今天在论坛看到有人提问,如何让两个按钮平分一行空间。
大概效果如下:
有人说,设置宽度固定大小,这样应该可以达到效果(本人没试),但是如果使用不同分辨率的手机来看,肯定会有问题,影响用户体验。
也有人说,设置宽度属性为 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控件布局】Layout 水平平分空间、垂直平分空间
- 【Android】Layout 水平平分空间、垂直平分空间
- 【Android】Layout 水平平分空间、垂直平分空间
- 【Android控件布局】Layout 水平平分空间、垂直平分空间
- 【Android控件布局】Layout 水平平分空间、垂直平分空间
- Android 水平平分布局和垂直平分布局
- android 水平平分两个按钮
- Android 实现九宫格(GridView中各项平分空间)
- RelativeLayout布局下实现控件平分空间
- RelativeLayout 如何实现平分布局空间
- 初学安卓平分空间排列相同内嵌布局
- Android GridView水平平均分配(根据numColumns平分适配)
- android linearlayout 平分一行
- Android XML视图 平分效果
- android中多个LinearLayout平分布局
- 平分七筐鱼
- 平分蛋糕
- (重要)项目整合nutch索引与查询过程记录
- C 关于内存
- 巧用导购销售技巧
- 是不是到了突破自我的时候了
- 算法导论习题8-3—排序不同长度的数据项
- 【Android】Layout 水平平分空间、垂直平分空间
- PHP执行linux系统命令的几个基本函数
- Launcher4 中widget的选择
- linux运行程序缺少库的解决方法
- FLEX XML、XMLList、XMLListCollection、ArrayCollection关系转换
- Android HAL(硬件抽象层)介绍以及调用 .
- [linux系统] kickstart配置文件ks.cfg详解
- 算法:java 实现单链表的反转
- 分享一个基于D3.js的互动可重用图表javascript类库 - NVD3.js