android 布局总结



转自http://www.apkbus.com/android-45156-1-1.html

本帖最后由 北小生 于 2012-5-11 02:10 编辑

01.jpg (27.68 KB, 下载次数: 39)

下载附件  保存到相册

2012-5-10 23:50 上传




     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实例的框架图,这个例子使用了布局套布局来实现如下图所示的框架效果。


2.jpg (61.12 KB, 下载次数: 43)

下载附件  保存到相册

2012-5-11 01:00 上传


    其中有三个LinearLayout 布局,最外层的 LinearLayout 中包含两个 LinearLayout 将界面分为上下两个部分,上面的 LinearLayout 中使用垂直布局将三个TextView纵向排列,下面的 LinearLayout 中使用水平布局将三个TextView横行排列。
    其中注意的是 android:layout_weight,用于给一个线性布局中的诸多视图的重要度赋值,所有的view的layout_weight缺省值都是为0,意味着他们只在屏幕上占据它们需要显示的空间大小。activity根据这个View的比0大的layout_weight值来划分剩余的空间和其它Views定义的layout_weight也按比例进行空间的划分。


4.png (22.61 KB, 下载次数: 32)

下载附件  保存到相册

2012-5-11 01:57 上传

  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 ,图片一层层覆盖到界面上。

5.png (14.05 KB, 下载次数: 38)

下载附件  保存到相册

2012-5-11 02:03 上传

  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>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值