Android五大布局(一)LinearLayout.FrameLayout

Android的界面是有布局和组件协同完成的,布局好比是建筑里的框架,而组件则相当于建筑里的砖瓦。组件按照布局的要求依次排列,就组成了用户所看见的界面。Android的五大布局分别是LinearLayout(线性布局)、FrameLayout(单帧布局)、RelativeLayout(相对布局)、AbsoluteLayout(绝对布局)和TableLayout(表格布局)。 
  
    Android 众多的布局属性详解 
    http://www.open-open.com/lib/view/open1328686184311.html 

   LinearLayout: 
    线性布局,这个东西,从外框上可以理解为一个div,他首先是一个一个从上往下罗列在屏幕上。每一个LinearLayout里面又可分为垂直布局(android:orientation="vertical")和水平布局(android:orientation="horizontal" )。当垂直布局时,每一行就只有一个元素,多个元素依次垂直往下;水平布局时,只有一行,每一个元素依次向右排列。 
  如果搭建两行两列的结构,通常的方式是先垂直排列两个元素,每一个元素里再包含一个LinearLayout进行水平排列。 
  LinearLayout中的子元素属性android:layout_weight生效,它用于描述该子元素在剩余空间中占有的大小比例。加入一行只有一个文本框,那么它的默认值就为0,如果一行中有两个等长的文本框,那么他们的android:layout_weight值可以是同为1。如果一行中有两个不等长的文本框,那么他们的android:layout_weight值分别为1和2,那么第一个文本框将占据剩余空间的三分之二,第二个文本框将占据剩余空间中的三分之一。android:layout_weight遵循数值越小,重要度越高的原则。 


显示效果如下: 



main.xml: 
Java代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3.     <LinearLayout  
  4.         xmlns:android="http://schemas.android.com/apk/res/android"  
  5.         android:layout_width="fill_parent"  
  6.         android:layout_height="fill_parent"  
  7.         android:orientation="vertical" >  
  8.   
  9.         <TextView  
  10.             android:layout_width="fill_parent"  
  11.             android:layout_height="wrap_content"  
  12.             android:background="#ff000000"  
  13.             android:text="@string/hello" />  
  14.   
  15.         <LinearLayout  
  16.             android:layout_width="fill_parent"  
  17.             android:layout_height="fill_parent"  
  18.             android:orientation="horizontal" >  
  19.   
  20.             <TextView  
  21.                 android:layout_width="fill_parent"  
  22.                 android:layout_height="wrap_content"  
  23.                 android:layout_weight="1"  
  24.                 android:background="#ff654321"  
  25.                 android:text="1" />  
  26.   
  27.             <TextView  
  28.                 android:layout_width="fill_parent"  
  29.                 android:layout_height="wrap_content"  
  30.                 android:layout_weight="2"  
  31.                 android:background="#fffedcba"  
  32.                 android:text="2" />  
  33.         </LinearLayout>  
  34.     </LinearLayout>  



frameLayout:
 
  FrameLayout是五大布局中最简单的一个布局,在这个布局中,整个界面被当成一块空白备用区域,所有的子元素都不能被指定放置的位置,它们统统放于这块区域的左上角,并且后面的子元素直接覆盖在前面的子元素之上,将前面的子元素部分和全部遮挡。显示效果如下,第一个TextView被第二个TextView完全遮挡,第三个TextView遮挡了第二个TextView的部分位置。 
  这个布局也可以看成是墙脚堆东西,有一个四方的矩形的左上角墙脚,我们放了第一个东西,要再放一个,那就在放在原来放的位置的上面,这样依次的放,会盖住原来的东西。这个布局比较简单,也只能放一点比较简单的东西 




Java代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3.     <FrameLayout  
  4.         xmlns:android="http://schemas.android.com/apk/res/android"  
  5.         android:layout_width="fill_parent"  
  6.         android:layout_height="fill_parent"  
  7.         android:orientation="vertical" >  
  8.   
  9.         <TextView  
  10.             android:layout_width="fill_parent"  
  11.             android:layout_height="fill_parent"  
  12.             android:background="#ff000000"  
  13.             android:gravity="center"  
  14.             android:text="1" />  
  15.   
  16.         <TextView  
  17.             android:layout_width="fill_parent"  
  18.             android:layout_height="fill_parent"  
  19.             android:background="#ff654321"  
  20.             android:gravity="center"  
  21.             android:text="2" />  
  22.   
  23.         <TextView  
  24.             android:layout_width="50dp"  
  25.             android:layout_height="50dp"  
  26.             android:background="#fffedcba"  
  27.             android:gravity="center"  
  28.             android:text="3" />  
  29.     </FrameLayout>  


FrameLayout就像web中iframe框架一样,是一个框架布局样式,可以用include标签载入定义的另一个layout文件,现在用一个ProgressBar的例子学习一下 FrameLayout。 

首先,我们创建一个布局文件,res/layout/fullscreen_loading.xml, 其内容如下: 

Java代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3. xmlns:android="http://schemas.android.com/apk/res/android"  
  4. android:orientation="horizontal"  
  5. android:gravity="center_vertical|center_horizontal"  
  6. android:layout_width="fill_parent"  
  7. android:layout_height="fill_parent  
  8. android:id="@+id/fullscreen_loading"  
  9. android:visibility="gone"  
  10. >  
  11. <ProgressBar  
  12. android:layout_gravity="center_vertical"  
  13. android:layout_width="wrap_content"  
  14. android:layout_height="wrap_content"  
  15. style="?android:attr/progressBarStyleSmall"  
  16. >  
  17. </ProgressBar>  
  18. <TextView  
  19. android:layout_width="wrap_content"  
  20. android:layout_height="wrap_content"  
  21. android:layout_marginLeft="5.0dip"  
  22. android:text="loading..."  
  23. >  
  24. </TextView>  
  25. </LinearLayout>  


然后在main.xml 把它include 进来 
Java代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <FrameLayout  
  3. xmlns:android="http://schemas.android.com/apk/res/android"  
  4. android:layout_width="fill_parent"  
  5. android:layout_height="fill_parent"  
  6. >  
  7. <LinearLayout  
  8. android:orientation="vertical"  
  9. android:visibility="gone"  
  10. android:layout_width="fill_parent"  
  11. android:layout_height="fill_parent"  
  12. >  
  13. </LinearLayout>  
  14. <include  
  15. android:visibility="visible"  
  16. android:layout_width="fill_parent"  
  17. android:layout_height="fill_parent"  
  18. layout="@layout/fullscreen_loading"  
  19. >  
  20. </include>  
  21. </FrameLayout>  


运行的效果为: 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值