http://blog.csdn.net/android_tutor/article/details/4779097

Android五大布局对象,它们分别是 FrameLayout(框架布局:不知道是不是这么翻译的), LinearLayout (线性布局), AbsoluteLayout(绝对布局), RelativeLayout(相对布局), TableLayout(表格布局). FrameLayout: FrameLayout是最简单的一个布局对象。它被定制为你屏幕上的一个空白备用区域,之后你可以在其中填充一个单一对象 — 比如,一张你要发布的图片。所有的子元素将会固定在屏幕的左上角;你不能为FrameLayout中的一个子元素指定一个位置。后一个子元素将会直接在前 一个子元素之上进行覆盖填充,把它们部份或全部挡住(除非后一个子元素是透明的)。 我们看一下效果图: <ignore_js_op style="word-wrap: break-word; "><img id="aimg_3286" aid="3286" src="http://www.apkbus.com/data/attachment/forum/201105/16/083444g087922f266fzy96.jpg" zoomfile="data/attachment/forum/201105/16/083444g087922f266fzy96.jpg" file="data/attachment/forum/201105/16/083444g087922f266fzy96.jpg" class="zoom" width="321" inpost="1" alt="1.jpg" title="1.jpg" lazyloaded="true" style="word-wrap: break-word; cursor: pointer; "></ignore_js_op> 其中 Main.xml代码如下:
  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. > <!-- 我们在这里加了一个Button按钮 -->
  6. <Button
  7. android:text="button"
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. />
  11. <TextView
  12. android:text="textview"
  13. android:textColor="#0000ff"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. />
  17. </FrameLayout>
复制代码


LinearLayout: LinearLayout以你为它设置的垂直或水平的属性值,来排列所有的子元素。所有的子元素都被堆放在其它元素之后,因此一个垂直列表的每一行只会有 一个元素,而不管他们有多宽,而一个水平列表将会只有一个行高(高度为最高子元素的高度加上边框高度)。LinearLayout保持子元素之间的间隔以 及互相对齐(相对一个元素的右对齐、中间对齐或者左对齐)。 LinearLayout还支持为单独的子元素指定 weight。好处就是允许子元素可以填充屏幕上的剩余空间。这也避免了在一个大屏幕中,一串小对象挤 成一堆的情况,而是允许他们放大填充空白。子元素指定一个 weight值,剩余的空间就会按这些子元素指定的 weight比例分配给这些子元素。默认的 weight值为0。例如,如果有三个文本框,其中两个指定了 weight值为1,那么,这两个文本框将等比例地放大,并填满剩余的空间,而第三个文本框 不会放大。 我们看一下效果图: <ignore_js_op style="word-wrap: break-word; "><img id="aimg_3287" aid="3287" src="http://www.apkbus.com/data/attachment/forum/201105/16/083446jn7unffn1fp7jp4p.jpg" zoomfile="data/attachment/forum/201105/16/083446jn7unffn1fp7jp4p.jpg" file="data/attachment/forum/201105/16/083446jn7unffn1fp7jp4p.jpg" class="zoom" width="320" inpost="1" alt="2.jpg" title="2.jpg" lazyloaded="true" _load="1" style="word-wrap: break-word; cursor: pointer; "></ignore_js_op> 其中 Main.xml代码如下:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent">
  6. <LinearLayout
  7. android:orientation="vertical"
  8. android:layout_width="fill_parent"
  9. android:layout_height="fill_parent"
  10. android:layout_weight="2">
  11. <TextView
  12. android:text="Welcome to Mr Wei's blog"
  13. android:textSize="15pt"
  14. android:layout_width="fill_parent"
  15. android:layout_height="wrap_content"
  16. />
  17. </LinearLayout>
  18. <LinearLayout
  19. android:orientation="horizontal"
  20. android:layout_width="fill_parent"
  21. android:layout_height="fill_parent"
  22. android:layout_weight="1">

  23. <TextView
  24. android:text="red" android:gravity="center_horizontal" //这里字水平居中
  25. android:background="#aa0000"
  26. android:layout_width="wrap_content"
  27. android:layout_height="fill_parent"
  28. android:layout_weight="1"/>
  29. <TextView
  30. android:text="green"
  31. android:gravity="center_horizontal "
  32. android:background="#00aa00"
  33. android:layout_width="wrap_content"
  34. android:layout_height="fill_parent"
  35. android:layout_weight="1"/>
  36. </LinearLayout>
  37. </LinearLayout>
复制代码

AbsoluteLayout: AbsoluteLayout可以让子元素指定准确的x/y坐标值,并显示在屏幕上。(0, 0)为左上角,当向下或向右移动时,坐标值将变大。 AbsoluteLayout没有页边框,允许元素之间互相重叠(尽管不推荐)。我们通常不推荐使用 AbsoluteLayout,除非你有正当理由要使用它,因为它使界面代码太过刚性,以至于在不同的设备上可能不能很好地工作。 我们看一下效果图: <ignore_js_op style="word-wrap: break-word; "><img id="aimg_3288" aid="3288" src="http://www.apkbus.com/data/attachment/forum/201105/16/083447rj59crpiyrfbretm.jpg" zoomfile="data/attachment/forum/201105/16/083447rj59crpiyrfbretm.jpg" file="data/attachment/forum/201105/16/083447rj59crpiyrfbretm.jpg" class="zoom" width="321" inpost="1" alt="3.jpg" title="3.jpg" lazyloaded="true" _load="1" style="word-wrap: break-word; cursor: pointer; "></ignore_js_op> 其中 Main.xml代码如下:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <EditText
  8. android:text="Welcome to Mr Wei's blog"
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. />
  12. <Button
  13. android:layout_x="250px" //设置按钮的X坐标
  14. android:layout_y="40px" //设置按钮的Y坐标
  15. android:layout_width="70px" //设置按钮的宽度
  16. android:layout_height="wrap_content"
  17. android:text="Button"
  18. />
  19. </AbsoluteLayout>
复制代码

RelativeLayout: RelativeLayout允许子元素指定他们相对于其它元素或父元素的位置(通过 ID指定)。因此,你可以以右对齐,或上下,或置于屏幕中央的形式来 排列两个元素。元素按顺序排列,因此如果第一个元素在屏幕的中央,那么相对于这个元素的其它元素将以屏幕中央的相对位置来排列。如果使用 XML来指定这个 layout,在你定义它之前,被关联的元素必须定义。 让我们看一下效果图: <ignore_js_op style="word-wrap: break-word; "><img id="aimg_3289" aid="3289" src="http://www.apkbus.com/data/attachment/forum/201105/16/08344837wk855wwg11ghdf.jpg" zoomfile="data/attachment/forum/201105/16/08344837wk855wwg11ghdf.jpg" file="data/attachment/forum/201105/16/08344837wk855wwg11ghdf.jpg" class="zoom" width="323" inpost="1" alt="4.jpg" title="4.jpg" lazyloaded="true" _load="1" style="word-wrap: break-word; cursor: pointer; "></ignore_js_op> 其中 Main.xml代码如下:
  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. <TextView
  6. android:id="@+id/label"
  7. android:layout_width="fill_parent"
  8. android:layout_height="wrap_content"
  9. android:text="Welcome to Mr Wei's blog:"/>
  10. <EditText
  11. android:id="@+id/entry"
  12. android:layout_width="fill_parent"
  13. android:layout_height="wrap_content"
  14. android:layout_below="@id/label"/>
  15. <Button
  16. android:id="@+id/ok"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:layout_below="@id/entry"
  20. android:layout_alignParentRight="true"
  21. android:layout_marginLeft="10dip"
  22. android:text="OK" />
  23. <Button
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:layout_toLeftOf="@id/ok"
  27. android:layout_alignTop="@id/ok"
  28. android:text="Cancel" />
  29. </RelativeLayout>
复制代码
TableLayout: TableLayout将子元素的位置分配到行或列中。一个 TableLayout由许多的 TableRow组成,每个 TableRow都会定义一个 row(事实上,你可以定义其它的子对象,这在下面会解释到)。 TableLayout容器不会显示 rowcloumnscell的边框线。每个 row拥有0个或多个的 cell;每个 cell拥有一个 View对象。表格由列和行组成许多的单元格。表格允许单元格为空。单元格不能跨列,这与 HTML中的不一样。 下面让我们看一下效果图: <ignore_js_op style="word-wrap: break-word; "><img id="aimg_3285" aid="3285" src="http://www.apkbus.com/data/attachment/forum/201105/16/0834439z572c3ou222qz1b.jpg" zoomfile="data/attachment/forum/201105/16/0834439z572c3ou222qz1b.jpg" file="data/attachment/forum/201105/16/0834439z572c3ou222qz1b.jpg" class="zoom" width="320" inpost="1" alt="5.jpg" title="5.jpg" lazyloaded="true" _load="1" style="word-wrap: break-word; cursor: pointer; "></ignore_js_op> 其中 Main.xml代码如下:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent" android:layout_height="fill_parent"
  4. android:stretchColumns="1">
  5. <TableRow>
  6. <TextView android:layout_column="1" android:text="Open..." />
  7. <TextView android:text="Ctrl-O" android:gravity="right" />
  8. </TableRow>
  9. <TableRow>
  10. <TextView android:layout_column="1" android:text="Save..." />
  11. <TextView android:text="Ctrl-S" android:gravity="right" />
  12. </TableRow>
  13. <View android:layout_height="2dip" android:background="#FF909090" /> //这里是上图中的分隔线
  14. <TableRow>
  15. <TextView android:text="X" />
  16. <TextView android:text="Export..." />
  17. <TextView android:text="Ctrl-E" android:gravity="right " />
  18. </TableRow>
  19. <View android:layout_height="2dip" android:background="#FF909090" />
  20. <TableRow>
  21. <TextView android:layout_column="1" android:text="Quit"
  22. android:padding="3dip" />
  23. </TableRow>
  24. </TableLayout>
复制代码

更多相关文章

  1. Test
  2. Android(安卓)11适配指南之Toast解析
  3. android 全屏 webview 加载的h5的输入框,被键盘遮挡的解决
  4. Android——GSON解析JSON
  5. android APP隐私政策弹框的实现代码实例
  6. android手机打电话代码分析
  7. Android(安卓)Platform 3.0 SDK和Eclipse ADT安装记录三
  8. android
  9. Android开发之dagger.android--Activity

随机推荐

  1. Android对象序列化(Activity之间传递对象,P
  2. Android圆形控件
  3. Android共享元素
  4. Android最快的模拟器Genymotion试用小结
  5. Android判断程序是否第一次运行
  6. Android闹钟设置的解决方案
  7. Android中hybrid开发的基础知识
  8. android基础学习--->adapter那点事儿
  9. Android中有几种数据存储方式,每种方式有
  10. Android(安卓)ApiDemo学习(五)Animation—