Android布局是应用界面开发的重要一环,在Android中,共有五种布局方式,分别是:LinearLayout(线性布局),FrameLayout(单帧布局),AbsoluteLayout(绝对布局),RelativeLayout(相对布局),TableLayout(表格布局)。
下面先分别介绍一下每种布局的基本概念,再用实例演示:
  • LinearLayout:线性布局,可分为垂直布局(android:orientation="vertical")和水平布局(android:orientation="horizontal" ),在LinearLayout里面可以放多个控件,但是一行(列)只能放一个控件。
  • FrameLayout:单帧布局,所有控件都放置在屏幕左上角(0,0),可以放多个控件,但是会按控件定义的先后顺序依次覆盖,后一个会直接覆盖在前一个之上显示,如果后放的比之前的大,会把之前的全部盖住(类似于一层层的纸张)。
  • AbsoluteLayout:绝对布局,可以直接指定子控件的绝对位置(例如: android:layout_x="60px" android:layout_y="32px" ),这种布局简单直接,但是由于手机的分辨率大小不统一,绝对布局的适应性比较差。
  • RelativeLayout:相对布局,其子控件是根据所设置的参照控件来进行布局的,设置的参照控件可以是父控件,也可以使其他的子控件。
  • TableLayout:表格布局,是以行列的形式来管理子控件的,在表格布局中的每一行可以是一个View控件或者是一个TableRow控件。而TableRow控件中还可以添加子控件。
=============================下面是实例===================== ===== LinearLayout:线性布局 LinearLayout实例的框架图,这个例子使用了布局套布局来实现如下图所示的框架效果。

其中有三个LinearLayout 布局,最外层的 LinearLayout 中包含两个 LinearLayout 将界面分为上下两个部分,上面的 LinearLayout 中使用垂直布局将三个TextView纵向排列,下面的 LinearLayout 中使用水平布局将三个TextView横行排列。 其中注意的是 android:layout_weight,用于给一个线性布局中的诸多视图的重要度赋值,所有的view的layout_weight缺省值都是为0,意味着他们只在屏幕上占据它们需要显示的空间大小。activity根据这个View的比0大的layout_weight值来划分剩余的空间和其它Views定义的layout_weight也按比例进行空间的划分。
android 五大布局经典演绎_第1张图片
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns: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. >
  7. <LinearLayout
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:orientation="vertical"
  11. android:layout_weight="1"
  12. >
  13. <TextView
  14. android:text="第一行"
  15. android:gravity="center"
  16. android:textSize="24px"
  17. android:background="#0066ff"
  18. android:layout_width="fill_parent"
  19. android:layout_height="wrap_content"
  20. android:layout_weight="1"
  21. />
  22. <TextView
  23. android:text="第二行"
  24. android:gravity="center"
  25. android:textSize="24px"
  26. android:background="#ffcc00"
  27. android:layout_width="fill_parent"
  28. android:layout_height="wrap_content"
  29. android:layout_weight="1"
  30. />
  31. <TextView
  32. android:text="第三行"
  33. android:gravity="center"
  34. android:textSize="24px"
  35. android:background="#33cc00"
  36. android:layout_width="fill_parent"
  37. android:layout_height="wrap_content"
  38. android:layout_weight="1"
  39. />
  40. </LinearLayout>
  41. <LinearLayout
  42. android:layout_width="fill_parent"
  43. android:layout_height="wrap_content"
  44. android:orientation="horizontal"
  45. android:layout_weight="1"
  46. >
  47. <TextView
  48. android:text="第一列"
  49. android:gravity="center"
  50. android:textSize="24px"
  51. android:background="#33cc00"
  52. android:layout_width="wrap_content"
  53. android:layout_height="fill_parent"
  54. android:layout_weight="1"
  55. />
  56. <TextView
  57. android:text="第二列"
  58. android:gravity="center"
  59. android:textSize="24px"
  60. android:background="#ffcc00"
  61. android:layout_width="wrap_content"
  62. android:layout_height="fill_parent"
  63. android:layout_weight="1"
  64. />
  65. <TextView
  66. android:text="第三列"
  67. android:gravity="center"
  68. android:textSize="24px"
  69. android:background="#0066ff"
  70. android:layout_width="wrap_content"
  71. android:layout_height="fill_parent"
  72. android:layout_weight="1"
  73. />
  74. </LinearLayout>
  75. </LinearLayout>
复制代码 FrameLayout:单帧布局
在FrameLayout中放了 三个 ImageView 分别引用图片c b a ,图片一层层覆盖到界面上。
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent" >
  5. <ImageView
  6. android:src="@drawable/c"
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. />
  10. <ImageView
  11. android:src="@drawable/b"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. />
  15. <ImageView
  16. android:src="@drawable/a"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. />
  20. </FrameLayout>

更多相关文章

  1. 浅谈Android常用控件
  2. Android 自定义控件实现刮刮卡效果 真的就只是刮刮卡么
  3. Android shap 控件美化
  4. 13、从头学Android之RelativeLayout相对布局
  5. 五成Android设备要向微软支付专利费
  6. [Android] ListView (普通列表控件) 的基本使用方法
  7. 自定义控件及效果

随机推荐

  1. Android 检测网络连接状态
  2. Android 基础总结:开篇
  3. ListView 列表视图
  4. Android问题笔记
  5. Android中自定义switch控件样式
  6. Android理解:显式和隐式Intent
  7. Android中子布局填充ScrollView
  8. 【Android】AIDL介绍和实例讲解
  9. 把应用跑在android上
  10. android 将鼠标右键点击事件改为点击后返