现在的位置: 首页 > 综合 > 正文

【Android】Layout 水平平分空间、垂直平分空间

2013年08月17日 ⁄ 综合 ⁄ 共 3114字 ⁄ 字号 评论关闭

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

 

大概效果如下:

 

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

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

 

那么有什么解决办法呢?

 

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

 

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

  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 带的布局例子,水平平分、垂直平分都有:

  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" />  

抱歉!评论已关闭.