Android系统五大布局详解Layout

  我们知道Android系统应用程序一般是由多个Activity组成,而这些Activity以视图的形式展现在我们面前, 视图都是由一个一个的组件构成的。组件就是我们常见的Button、TextEdit等等。那么我们平时看到的Android手机中那些漂亮的界面是怎么显示 出来的呢?这就要用到Android的布局管理器了,网上有人比喻的很好:布局好比是建筑里的框架,组件按照布局的要求依次排列,就组成了用于看见的漂亮界面了。

      在分析布局之前,我们首先看看控件:Android中任何可视化的控件都是从android.veiw.View继承而来的,系统提供了两种方法来设置视图:第一种也是我们最常用的的使用XML文件来配置View的相关属性,然后在程序启动时系统根据配置文件来创建相应的View视图。第二种是我们在代码中直接使用相应的类来创建视图。

     如何使用XML文件定义视图:

      每个Android项目的源码目录下都有个res/layout目录,这个目录就是用来存放布局文件的。布局文件一般以对应activity的名字命名,以 .xml 为后缀。在xml中为创建组件时,需要为组件指定id,如:android:id="@+id/名字"系统会自动在gen目录下创建相应的R资源类变量。 

    如何在代码中使用视图:  

     在代码中创建每个Activity时,一般是在onCreate()方法中,调用setContentView()来加载指定的xml布局文件,然后就可以通过findViewById()来获得在布局文件中创建的相应id的控件了,如Button等。

 如:

  1. private Button btnSndMag;  
  2. public void onCreate(Bundle savedInstanceState) {  
  3.     super.onCreate(savedInstanceState);  
  4.     setContentView(R.layout.main);  // 加载main.xml布局文件  
  5.     btnSndMag = (Button)this.findViewById(R.id.btnSndMag); // 通过id找到对于的Button组件  
  6.     ....  
  7. }  
  

  下面我们来介绍Android系统中为我们提供的五大布局:LinearLayout(线性布局)、FrameLayout(单帧布局)、AbsoluteLayout(绝对布局)、TablelLayout(表格布局)、RelativeLayout(相对布局)。其中最常用的的是LinearLayout、TablelLayout和RelativeLayout。这些布局都可以嵌套使用。

(1)LinearLayout 线性布局

  线性布局是按照水平或垂直的顺序将子元素(可以是控件或布局)依次按照顺序排列,每一个元素都位于前面一个元素之后。线性布局分为两种:水平方向和垂直方向的布局。分别通过属性android:orientation="vertical" 和 android:orientation="horizontal"来设置。

 android:layout_weight 表示子元素占据的空间大小的比例,有人说这个值大小和占据空间成正比,有人说反比。我在实际应用中设置和网上资料显示的刚好相反,这个问题后面会专门写一篇文章来分析。现在我们只需要按照正比例来设置就可以。 

例如下面我们实现一个如图所示的简易计算器界面:


代码:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:background="#FFFFFF"  
  7.     tools:context=".MainActivity" >  
  8.   
  9.     // 这里第一行显示标签为一个水平布局  
  10.     <LinearLayout  
  11.         android:layout_width="match_parent"  
  12.         android:layout_height="wrap_content"  
  13.         android:orientation="horizontal" >  
  14.         <EditText  
  15.             android:id="@+id/msg"  
  16.             android:inputType="number"  
  17.             android:layout_width="match_parent"  
  18.             android:layout_height="wrap_content"  
  19.             android:text="">  
  20.         </EditText>      
  21.     </LinearLayout>  
  22.       
  23.     // 第二行为 mc m+ m- mr 四个Button构成一个水平布局  
  24.     <LinearLayout  
  25.         android:layout_width="match_parent"  
  26.         android:layout_height="wrap_content"  
  27.         android:orientation="horizontal" >  
  28.         <Button  
  29.             android:layout_width="match_parent"  
  30.             android:layout_height="wrap_content"  
  31.             android:text="mc" android:layout_weight="1">  
  32.         </Button>  
  33.         <Button  
  34.             android:layout_width="match_parent"  
  35.             android:layout_height="wrap_content"  
  36.             android:text="m+" android:layout_weight="1">  
  37.         </Button>  
  38.         <Button  
  39.             android:layout_width="match_parent"  
  40.             android:layout_height="wrap_content"  
  41.             android:text="m-" android:layout_weight="1">  
  42.         </Button>  
  43.         <Button  
  44.             android:layout_width="match_parent"  
  45.             android:layout_height="wrap_content"  
  46.             android:text="mr" android:layout_weight="1">  
  47.         </Button>  
  48.     </LinearLayout>  
  49.       
  50.     // 同上 C +/-  / * 四个Button构成一个水平布局  
  51.       <LinearLayout  
  52.           android:layout_width="match_parent"  
  53.           android:layout_height="wrap_content"  
  54.           android:orientation="horizontal" >  
  55.           <Button  
  56.               android:layout_width="match_parent"  
  57.               android:layout_height="wrap_content"  
  58.               android:layout_weight="1"  
  59.               android:text="C" >  
  60.           </Button>  
  61.           <Button  
  62.               android:layout_width="match_parent"  
  63.               android:layout_height="wrap_content"  
  64.               android:layout_weight="1"  
  65.               android:text="+/-" >  
  66.           </Button>  
  67.           <Button  
  68.               android:layout_width="match_parent"  
  69.               android:layout_height="wrap_content"  
  70.               android:layout_weight="1"  
  71.               android:text="/" >  
  72.           </Button>  
  73.           <Button  
  74.               android:layout_width="match_parent"  
  75.               android:layout_height="wrap_content"  
  76.               android:layout_weight="1"  
  77.               android:text="*" >  
  78.           </Button>  
  79.       </LinearLayout>  
  80.       
  81.       <LinearLayout  
  82.         android:layout_width="match_parent"  
  83.         android:layout_height="wrap_content"  
  84.         android:orientation="horizontal" >  
  85.         <Button  
  86.             android:layout_width="match_parent"  
  87.             android:layout_height="wrap_content"  
  88.             android:text="7" android:layout_weight="1">  
  89.         </Button>  
  90.         <Button  
  91.             android:layout_width="match_parent"  
  92.             android:layout_height="wrap_content"  
  93.             android:text="8" android:layout_weight="1">  
  94.         </Button>  
  95.         <Button  
  96.             android:layout_width="match_parent"  
  97.             android:layout_height="wrap_content"  
  98.             android:text="9" android:layout_weight="1">  
  99.         </Button>  
  100.         <Button  
  101.             android:layout_width="match_parent"  
  102.             android:layout_height="wrap_content"  
  103.             android:text="-" android:layout_weight="1">  
  104.         </Button>  
  105.     </LinearLayout>  
  106.       
  107.     <LinearLayout  
  108.         android:layout_width="match_parent"  
  109.         android:layout_height="wrap_content"  
  110.         android:orientation="horizontal" >  
  111.         <Button  
  112.             android:layout_width="match_parent"  
  113.             android:layout_height="wrap_content"  
  114.             android:layout_weight="1"  
  115.             android:text="4" >  
  116.         </Button>  
  117.         <Button  
  118.             android:layout_width="match_parent"  
  119.             android:layout_height="wrap_content"  
  120.             android:layout_weight="1"  
  121.             android:text="5" >  
  122.         </Button>  
  123.         <Button  
  124.             android:layout_width="match_parent"  
  125.             android:layout_height="wrap_content"  
  126.             android:layout_weight="1"  
  127.             android:text="6" >  
  128.         </Button>  
  129.         <Button  
  130.             android:layout_width="match_parent"  
  131.             android:layout_height="wrap_content"  
  132.             android:layout_weight="1"  
  133.             android:text="+" >  
  134.         </Button>  
  135.     </LinearLayout>  
  136.       
  137.     // 最外层是一个水平布局,由左边上面一行1 2 3三个Button,下面一行的0 . 两个Button 和 右边的=构成  
  138.      <LinearLayout android:orientation="horizontal"  
  139.         android:layout_width="match_parent"  
  140.         android:layout_height="wrap_content">  
  141.         // 这里 1 2 3 和 下面的 0 . 构成一个垂直布局  
  142.         <LinearLayout android:orientation="vertical"  
  143.             android:layout_weight="3"  
  144.             android:layout_width="wrap_content"  
  145.             android:layout_height="wrap_content">  
  146.             // 这里的 1 2 3 构成一个水平布局  
  147.             <LinearLayout android:orientation="horizontal"  
  148.                 android:layout_width="match_parent"  
  149.                 android:layout_height="wrap_content">  
  150.                 <Button  
  151.                     android:layout_width="wrap_content"  
  152.                     android:layout_height="wrap_content"  
  153.                     android:layout_weight="1"  
  154.                     android:text="1"></Button>  
  155.                 <Button  
  156.                     android:layout_width="wrap_content"  
  157.                     android:layout_height="wrap_content"  
  158.                     android:layout_weight="1"  
  159.                     android:text="2"></Button>  
  160.                 <Button  
  161.                     android:layout_width="wrap_content"  
  162.                     android:layout_height="wrap_content"  
  163.                     android:layout_weight="1"  
  164.                     android:text="3"></Button>  
  165.             </LinearLayout>  
  166.             // 这里的 0 和 . 构成一个水平布局,注意这里的android_weight参数设置  
  167.             <LinearLayout android:orientation="horizontal"  
  168.                 android:layout_width="match_parent"  
  169.                 android:layout_height="wrap_content">  
  170.                 <Button  
  171.                     android:layout_width="0px"  
  172.                     android:layout_height="wrap_content"  
  173.                     android:layout_weight="2"  
  174.                     android:text="0"></Button>  
  175.                 <Button  
  176.                     android:layout_width="0px"  
  177.                     android:layout_height="wrap_content"  
  178.                     android:layout_weight="1"  
  179.                     android:text="."></Button>  
  180.             </LinearLayout>     
  181.         </LinearLayout>  
  182.         // 这里一个单独Button构成的垂直布局  
  183.         <LinearLayout android:orientation="vertical"  
  184.             android:layout_weight="1"  
  185.             android:layout_width="wrap_content"  
  186.             android:layout_height="match_parent">  
  187.             <Button  
  188.                 android:layout_width="match_parent"  
  189.                 android:layout_height="match_parent"  
  190.                 android:text="="></Button>  
  191.         </LinearLayout>  
  192.      </LinearLayout>     
  193.   
  194. </LinearLayout>  


(2)TableLayout 表格布局

         表格布局,适用于多行多列的布局格式,每个TableLayout是由多个TableRow组成,一个TableRow就表示TableLayout中的每一行,这一行可以由多个子元素组成。实际上TableLayout和TableRow都是LineLayout线性布局的子类。但是TableRow的参数android:orientation属性值固定为horizontal,且android:layout_width=MATCH_PARENT,android:layout_height=WRAP_CONTENT。所以TableRow实际是一个横向的线性布局,且所以子元素宽度和高度一致。

       注意:在TableLayout中,单元格可以为空,但是不能跨列,意思是只能不能有相邻的单元格为空。

        在TableLayout布局中,一列的宽度由该列中最宽的那个单元格指定,而该表格的宽度由父容器指定。可以为每一列设置以下属性:

     Shrinkable  表示该列的宽度可以进行收缩,以使表格能够适应父容器的大小

     Stretchable 表示该列的宽度可以进行拉伸,以使能够填满表格中的空闲空间

     Collapsed  表示该列会被隐藏

TableLayout中的特有属性:

android:collapseColumns

        android:shrinkColumns

        android:stretchColumns = "0,1,2,3"// 表示产生4个可拉伸的列

Demo:我们想设计一个如下所以的一个三行三列的表格,但是第二行我们只想显示2个表格:




  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:shrinkColumns="0,1,2"  // 设置三列都可以收缩    
  5.     android:stretchColumns="0,1,2" // 设置三列都可以拉伸 如果不设置这个,那个显示的表格将不能填慢整个屏幕  
  6.     android:layout_width="fill_parent"  
  7.     android:layout_height="fill_parent" >      
  8.     <TableRow android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content">  
  10.         <Button android:gravity="center"  
  11.             android:padding="10dp"  
  12.             android:text="Button1">  
  13.         </Button>  
  14.         
  15.         <Button android:gravity="center"  
  16.             android:padding="10dp"  
  17.             android:text="Button2">  
  18.         </Button>  
  19.         <Button android:gravity="center"  
  20.             android:padding="10dp"  
  21.             android:text="Button3">  
  22.         </Button>  
  23.     </TableRow>  
  24.     <TableRow android:layout_width="fill_parent"  
  25.         android:layout_height="wrap_content">  
  26.         <Button android:gravity="center"  
  27.             android:padding="10dp"  
  28.             android:text="Button4">  
  29.         </Button>  
  30.         
  31.         <Button android:gravity="center"  
  32.             android:padding="10dp"  
  33.             android:text="Button5">  
  34.         </Button>  
  35.     </TableRow>  
  36.     <TableRow android:layout_width="fill_parent"  
  37.         android:layout_height="wrap_content">  
  38.         <Button android:gravity="center"  
  39.             android:padding="10dp"  
  40.             android:text="Button6">  
  41.         </Button>       
  42.         <Button android:gravity="center"  
  43.             android:padding="10dp"  
  44.             android:text="Button7">  
  45.         </Button>  
  46.         <Button android:gravity="center"  
  47.             android:padding="10dp"  
  48.             android:text="Button8">  
  49.         </Button>  
  50.     </TableRow>  
  51. </TableLayout>  


(3)RelativeLayout 相对布局

   RelativeLayout继承于android.widget.ViewGroup,其按照子元素之间的位置关系完成布局的,作为Android系统五大布局中最灵活也是最常用的一种布局方式,非常适合于一些比较复杂的界面设计。

   注意:在引用其他子元素之前,引用的ID必须已经存在,否则将出现异常。

常用的位置属性:

  

  1. android:layout_toLeftOf         该组件位于引用组件的左方  
  2. android:layout_toRightOf        该组件位于引用组件的右方  
  3. android:layout_above            该组件位于引用组件的上方  
  4. android:layout_below                该组件位于引用组件的下方  
  5. android:layout_alignParentLeft      该组件是否对齐父组件的左端  
  6. android:layout_alignParentRight     该组件是否齐其父组件的右端  
  7. android:layout_alignParentTop       该组件是否对齐父组件的顶部  
  8. android:layout_alignParentBottom    该组件是否对齐父组件的底部  
  9. android:layout_centerInParent       该组件是否相对于父组件居中  
  10. android:layout_centerHorizontal     该组件是否横向居中  
  11. android:layout_centerVertical       该组件是否垂直居中  

Demo:利用相对布局设计一个如下图所示的界面:


源码:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent" >  
  5.     <Button android:id="@+id/btn1"  
  6.         android:layout_width="wrap_content"  
  7.         android:layout_height="wrap_content"  
  8.         android:layout_centerInParent="true"  
  9.         android:layout_centerHorizontal="true"  
  10.         android:text="Button1"  
  11.         ></Button>  
  12.     <Button android:id="@+id/btn2"  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:layout_toLeftOf="@id/btn1"  
  16.         android:layout_above="@id/btn1"  
  17.         android:text="Button2"  
  18.         ></Button>  
  19.     <Button android:id="@+id/btn3"  
  20.         android:layout_width="wrap_content"  
  21.         android:layout_height="wrap_content"  
  22.         android:layout_toRightOf="@id/btn1"  
  23.         android:layout_above="@id/btn1"  
  24.         android:text="Button3"  
  25.         ></Button>  
  26.     <Button android:id="@+id/btn4"  
  27.         android:layout_width="wrap_content"  
  28.         android:layout_height="wrap_content"  
  29.         android:layout_toRightOf="@id/btn2"  
  30.         android:layout_toLeftOf="@id/btn3"  
  31.         android:layout_above="@id/btn2"  
  32.         android:text="Button4"  
  33.         ></Button>  
  34. </RelativeLayout>  

(4)FrameLayout 框架布局

 将所有的子元素放在整个界面的左上角,后面的子元素直接覆盖前面的子元素,所以用的比较少。

(5) AbsoluteLayou 绝对布局

   绝对布局中将所有的子元素通过设置android:layout_x 和 android:layout_y属性,将子元素的坐标位置固定下来,即坐标(android:layout_x, android:layout_y) ,layout_x用来表示横坐标,layout_y用来表示纵坐标。屏幕左上角为坐标(0,0),横向往右为正方,纵向往下为正方。实际应用中,这种布局用的比较少,因为Android终端一般机型比较多,各自的屏幕大小。分辨率等可能都不一样,如果用绝对布局,可能导致在有的终端上显示不全等。


除上面讲过之外常用的几个布局的属性:
(1)layout_margin 
用于设置控件边缘相对于父控件的边距
android:layout_marginLeft 
android:layout_marginRight
android:layout_marginTop
android:layout_marginBottom


(2) layout_padding 
用于设置控件内容相对于控件边缘的边距
android:layout_paddingLeft
android:layout_paddingRight
android:layout_paddingTop
android:layout_paddingBottom


(3) layout_width/height
用于设置控件的高度和宽度
wrap_content 内容包裹,表示这个控件的里面文字大小填充
fill_parent 跟随父窗口
match_parent


(4) gravity 
用于设置View组件里面内容的对齐方式
top bottom left   right  center等


(5) android:layout_gravity  
用于设置Container组件的对齐方式
android:layout_alignTop 本元素的上边缘和某元素的的上边缘对齐
android:layout_alignLeft 本元素的左边缘和某元素的的左边缘对齐
android:layout_alignBottom 本元素的下边缘和某元素的的下边缘对齐
android:layout_alignRight 本元素的右边缘和某元素的的右边缘对齐

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值