Android的五大布局分别是:

  1. LinearLayout(线性布局)
  2. FrameLayout(单帧布局)
  3. RelativeLayout(相对布局)
  4. AbsoluteLayout(绝对布局)
  5. TableLayout(表格布局)

接下来详细讲解每个布局

1、LinearLayout

  LinearLayout按照垂直或者水平的顺序依次排列子元素,每一个子元素都位于前一个元素之后。如果是垂直排列,那么将是一个N行单列的结构,每一行只会有一个元素,而不论这个元素的宽度为多少;如果是水平排列,那么将是一个单行N列的结构。如果搭建两行两列的结构,通常的方式是先垂直排列两个元素,每一个元素里再包含一个LinearLayout进行水平排列。

android:layout_weight:比重 即控件占的长度比例

代码实例1:子控件android:layout_width设置为0

        
  1. <LinearLayout
  2. android:layout_width="match_parent"
  3. android:layout_height="wrap_content">
  4. <TextView
  5. android:layout_width="0dp"
  6. android:layout_height="wrap_content"
  7. android:layout_weight="2"
  8. android:background="#cfcfcf"
  9. android:text="比重为2"/>
  10. <TextView
  11. android:layout_width="0dp"
  12. android:layout_height="wrap_content"
  13. android:layout_weight="1"
  14. android:background="#f0f0f0"
  15. android:text="比重为1"/>
  16. </LinearLayout>

效果图:

代码实例2:子控件android:layout_width设置为match_parent

        
  1. <LinearLayout
  2. android:layout_width="match_parent"
  3. android:layout_height="wrap_content">
  4. <TextView
  5. android:layout_width="match_parent"
  6. android:layout_height="wrap_content"
  7. android:layout_weight="2"
  8. android:background="#cfcfcf"
  9. android:text="比重为2"/>
  10. <TextView
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content"
  13. android:layout_weight="1"
  14. android:background="#f0f0f0"
  15. android:text="比重为1"/>
  16. </LinearLayout>

效果图:

从上面的例子可以看出 当layout_width设为0dp时 显示效果对应 使用比较方便

2、FrameLayout

FrameLayout是五大布局中最简单的一个布局,在这个布局中,整个界面被当成一块空白备用区域,所有的子元素都不能被指定放置的位置,它们统统放于这块区域的左上角,并且后面的子元素直接覆盖在前面的子元素之上,将前面的子元素部分和全部遮挡。

        
  1. <FrameLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical"
  6. tools:context=".MainActivity">
  7. <TextView
  8. android:layout_width="300dp"
  9. android:layout_height="300dp"
  10. android:background="#00FFFF"
  11. android:gravity="center"
  12. android:text="下"/>
  13. <TextView
  14. android:layout_width="120dp"
  15. android:layout_height="120dp"
  16. android:background="#1E90FF"
  17. android:gravity="center"
  18. android:text="中"/>
  19. <TextView
  20. android:layout_width="40dp"
  21. android:layout_height="40dp"
  22. android:background="#32CD32"
  23. android:gravity="center"
  24. android:text="上"/>
  25. </FrameLayout>

效果图:

3、AbsoluteLayout

AbsoluteLayout是绝对位置布局。在此布局中的子元素的android:layout_x和android:layout_y属性将生效,用于描述该子元素的坐标位置。屏幕左上角为坐标原点(0,0),第一个0代表横坐标,向右移动此值增大,第二个0代表纵坐标,向下移动,此值增大。在此布局中的子元素可以相互重叠。在实际开发中,通常不采用此布局格式,因为它的界面代码过于刚性,以至于有可能不能很好的适配各种终端。

* 新版本的sdk中不推荐使用

4、RelativeLayout

RelativeLayout按照各子元素之间的位置关系完成布局。在此布局中的子元素里与位置相关的属性将生效。例如android:layout_below, android:layout_above等。子元素就通过这些属性和各自的ID配合指定位置关系。注意在指定位置关系时,引用的ID必须在引用之前,先被定义,否则将出现异常。

RelativeLayout里常用的位置属性如下:
    android:layout_toLeftOf —— 该组件位于引用组件的左方
    android:layout_toRightOf —— 该组件位于引用组件的右方
    android:layout_above —— 该组件位于引用组件的上方
    android:layout_below —— 该组件位于引用组件的下方
   android:layout_alignParentLeft —— 该组件是否对齐父组件的左端
   android:layout_alignParentRight —— 该组件是否齐其父组件的右端
   android:layout_alignParentTop —— 该组件是否对齐父组件的顶部
   android:layout_alignParentBottom —— 该组件是否对齐父组件的底部
    android:layout_centerInParent —— 该组件是否相对于父组件居中
    android:layout_centerHorizontal —— 该组件是否横向居中
    android:layout_centerVertical —— 该组件是否垂直居中

RelativeLayout是Android五大布局结构中最灵活的一种布局结构,比较适合一些复杂界面的布局。

        
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical">
  6. <TextView
  7. android:id="@+id/text_01"
  8. android:layout_width="50dp"
  9. android:layout_height="50dp"
  10. android:layout_alignParentBottom="true"
  11. android:background="#ffffffff"
  12. android:gravity="center"
  13. android:text="1"/>
  14. <TextView
  15. android:id="@+id/text_02"
  16. android:layout_width="50dp"
  17. android:layout_height="50dp"
  18. android:layout_above="@id/text_01"
  19. android:layout_centerHorizontal="true"
  20. android:background="#ff654321"
  21. android:gravity="center"
  22. android:text="2"/>
  23. <TextView
  24. android:id="@+id/text_03"
  25. android:layout_width="50dp"
  26. android:layout_height="50dp"
  27. android:layout_above="@id/text_01"
  28. android:layout_toLeftOf="@id/text_02"
  29. android:background="#fffedcba"
  30. android:gravity="center"
  31. android:text="3"/>
  32. </RelativeLayout>

代码效果图:

5、TableLayout

TableLayout顾名思义,此布局为表格布局,适用于N行N列的布局格式。一个TableLayout由许多TableRow组成,一个TableRow就代表TableLayout中的一行。

  TableRow是LinearLayout的子类,它的android:orientation属性值恒为horizontal,并且它的android:layout_width和android:layout_height属性值恒为MATCH_PARENT和WRAP_CONTENT。所以它的子元素都是横向排列,并且宽高一致的。这样的设计使得每个TableRow里的子元素都相当于表格中的单元格一样。在TableRow中,单元格可以为空,但是不能跨列。

        
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <TableLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"android:layout_height="fill_parent"
  4. android:orientation="vertical">
  5. <TableRowandroid:layout_width="fill_parent"
  6. android:layout_height="wrap_content">
  7. <TextViewandroid:background="#ffffffff"android:gravity="center"
  8. android:padding="10dp"android:text="1"/>
  9. <TextViewandroid:background="#ff654321"android:gravity="center"
  10. android:padding="10dp"android:text="2"/>
  11. <TextViewandroid:background="#fffedcba"android:gravity="center"
  12. android:padding="10dp"android:text="3"/>
  13. </TableRow>
  14. <TableRowandroid:layout_width="fill_parent"
  15. android:layout_height="wrap_content">
  16. <TextViewandroid:background="#ff654321"android:gravity="center"
  17. android:padding="10dp"android:text="2"/>
  18. <TextViewandroid:background="#fffedcba"android:gravity="center"
  19. android:padding="10dp"android:text="3"/>
  20. </TableRow>
  21. <TableRowandroid:layout_width="fill_parent"
  22. android:layout_height="wrap_content">
  23. <TextViewandroid:background="#fffedcba"android:gravity="center"
  24. android:padding="10dp"android:text="3"/>
  25. <TextViewandroid:background="#ff654321"android:gravity="center"
  26. android:padding="10dp"android:text="2"/>
  27. <TextViewandroid:background="#ffffffff"android:gravity="center"
  28. android:padding="10dp"android:text="1"/>
  29. </TableRow>
  30. </TableLayout>

效果图:

更多相关文章

  1. Android >> 26. RecyclerView(二)— 实现横向滚动和瀑布流布局
  2. android 基本组件
  3. Android例子源码集成安卓主流优秀第三方组件框架
  4. Android软键盘遮挡布局问题;
  5. Android对用五大布局
  6. Android使用GridLayout布局简单的计算器界面
  7. android:MATCH_PARENT是什么类型的布局
  8. android布局中容易混淆的几个属性
  9. Android StaggeredGridLayoutManager布局RecyclerView在滚动状态

随机推荐

  1. Android软件工程师之不归路
  2. android最佳实践(二)
  3. Android(安卓)4高级编程(第3版) 试读
  4. cocos2dx打包Android出现“未指定Android
  5. Android(安卓)中文API合集(3)(83篇)(chm
  6. android:layout_gravity和android:gravit
  7. 关于Android的selector小用法
  8. Android进阶——Android视图工作机制之me
  9. Android(安卓)源代码分享
  10. Android之我当年爬过的坑