Android五大布局

转自http://www.apkbus.com/blog-103989-39785.html

为了适应各式各样的界面风格,Android系统提供了5种布局,这5种布局分别是:

LinearLayout(线性布局)


TableLayout(表格布局)

RelativeLayout(相对布局)

AbsoluteLayout(绝对布局)

FrameLayout(框架布局)

利用这五种布局,可以在屏幕上将控件随心所欲的摆放,而且控件的大小和位置会随着屏幕大小的变化作出相应的调整。下面是这五个布局在View的继承体系中的关系:
160755mlplbli9qhpb7znz.jpg


一,LinearLayout(线性布局)


在一个方向上(垂直或水平)对齐所有子元素
一个垂直列表每行将只有一个子元素(无论它们有多宽)
一个水平列表只是一列的高度(最高子元素的高度来填充)

下面是一个简单的线性布局的例子:

1.jpg
2012-7-3 17:44 上传
下载附件 (5.55 KB)

<?xml version="1.0" encoding="UTF-8" ?>
<!--match_parent?   wrap_content 包裹 vertical垂直显示  horizontal 水平显示  fill_parent填充父类-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <EditText android:id="@+id/editText1"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"/>
    <LinearLayout android:id="@+id/linearLayout1"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:gravity="right">
        <Button
            android:id="@+id/button1"
            android:text="button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <Button
            android:id="@+id/button2"
            android:text="button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>
</LinearLayout>

最外层布局为垂直线性布局,宽度为整个屏幕(fill_parent),高度为刚好适合子控件(wrap_content)。然后依次添加一个EditText,一个水平布局的LinearLayout,在这个线性布局里面,摆放两个Button,该线性布局的gravity属性设置为”right”,所以里面的两个Button靠右显示。

二,TableLayout(表格布局)

把子元素放入到行与列中

不显示行、列或是单元格边界线
单元格不能横跨行,如HTML中一样
表格布局模型以行列的形式管理子控件,每一行为一个TableRow的对象,当然也可以是一个View的对象。TableRow可以添加子控件,每添加一个为一列。

android:layout_colum官方解释:The index of the column in which this child should be,也即是设置该控件在TableRow中所处的列。
android:layout_span官方解释:Defines how many columns this child should span,也即是设置该控件所跨越的列数。

android:collapseColumns官方解释:The 0 based index of the columns to collapse. The column indices must be separated by a comma: 1, 2, 5.也即是将TableLayout里面指定的列隐藏,若有多列需要隐藏,请用逗号将需要隐藏的列序号隔开。

android:stretchColumns官方解释:The 0 based index of the columns to stretch. The column indices must be separated by a comma: 1, 2, 5. You can stretch all columns by using the value “*” instead. Note that a column can be marked stretchable and shrinkable at the same time.也即是设置指定的列为可伸展的列,可伸展的列会尽量伸展以填满所有可用的空间,若有多列需要设置为可伸展,请用逗号将需要伸展的列序号隔开。

android:shrinkColumns官方解释:The 0 based index of the columns to shrink. The column indices must be separated by a comma: 1, 2, 5. You can shrink all columns by using the value “*” instead. 设置指定的列为可收缩的列。当可收缩的列太宽以至于让其他列显示不全时,会纵向延伸空间。当需要设置多列为可收缩时,将列序号用逗号隔开。

下面用一个例子简单说明TableLayout的用法:
1607553mn3z87qq7o9ii35.png

<?xml version="1.0" encoding="UTF-8"?>
<TableLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stretchColumns="1">
   <TableRow>
    <TextView
        android:layout_column="1"
        android:padding="3dip"
        android:text="Row1" />

    <TextView
        android:text="1"
        android:gravity="right"
        android:padding="3dip"/>
     </TableRow>
     <View
        android:layout_height="2dip"
        android:background="#FF909090" />
     <TableRow >
         <TextView android:text="*"
             android:padding="3dip"/>
         <TextView android:text="Row12"
             android:padding="3dip"/>
         <TextView  android:text="2"
             android:gravity="right"
             android:padding="3dip"/>
     </TableRow>
       <View
        android:layout_height="2dip"
        android:background="#FF909090" />
     <TextView android:layout_column="1"
         android:text="Row13"
         android:padding="3dip"/>
</TableLayout>

三、RelativeLayout(相对布局)

相对布局的子控件会根据它们所设置的参照控件和参数进行相对布局。参照控件可以是父控件,也可以是其它子控件,但是被参照的控件必须要在参照它的控件之前定义。下面是一个简单的例子:

160756qt0ua9hzt0779g22.png

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <AnalogClock
        android:id="@+id/aclock"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"/>
    <DigitalClock
        android:id="@+id/dclock"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/aclock"
        android:layout_alignLeft="@id/aclock"
        android:layout_marginLeft="40px"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="当前时间:"
        android:layout_toLeftOf="@id/dclock"
        android:layout_alignTop="@id/aclock"/>
</RelativeLayout>

四、AbsoluteLayout(绝对布局)

绝对布局的子控件需要指定相对于此坐标布局的横纵坐标值,否则将会像框架布局那样被排在左上角。手机应用需要适应不同的屏幕大小,而这种布局模型不能自适应屏幕尺寸大小,所以应用的相对较少。下面以一个例子简单说明绝对布局:

1607569ye2t0ew0ti9yef0.png

<?xml version="1.0" encoding="UTF-8"?>
<AbsoluteLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="10px"
        android:layout_y="10px"
        android:text="TextView"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="30px"
        android:layout_y="30px"
        android:text="TextView"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="50px"
        android:layout_y="50px"
        android:text="TextView"/>
</AbsoluteLayout>

五、FrameLayout(框架布局也叫帧布局)


框架布局是最简单的布局形式。所有添加到这个布局中的视图都以层叠的方式显示。第一个添加的控件被放在最底层,最后一个添加到框架布局中的视图显示在最顶层,上一层的控件会覆盖下一层的控件。这种显示方式有些类似于堆栈。下面举一个简单的例子


160756cn2qzk77zo1s9c29.png

  1. 02 <framelayout
  2. 03 android:layout_width="fill_parent" android:layout_height="fill_parent">
  3. 04 <linearlayout android:id="@+id/linearLayout1"
  4. 05 android:layout_height="match_parent"
  5. 06 android:layout_width="match_parent">
  6. 07 <button
  7. 08 android:id="@+id/button1"
  8. 09 android:layout_width="wrap_content"
  9. 10 android:layout_height="wrap_content">
  10. 11
  11. 12 <linearlayout
  12. 13 android:id="@+id/linearLayout3"
  13. 14 android:layout_height="match_parent"
  14. 15 android:gravity="bottom|right">
  15. 16 <button
  16. 17 android:id="@+id/button3"
  17. 18 android:layout_width="wrap_content"
  18. 19 android:layout_height="wrap_content">
  19. 20
  20. 21 <linearlayout
  21. 22 android:id="@+id/linearLayout2"
  22. 23 android:layout_width="match_parent"
  23. 24 android:gravity="right">
  24. 25 <button
  25. 26 android:id="@+id/button2"
  26. 27 android:layout_width="wrap_content"
  27. 28 android:layout_height="wrap_content">
  28. 29
  29. 30 <linearlayout
  30. 31 android:id="@+id/LinearLayout01"
  31. 32 android:layout_height="match_parent"
  32. 33 android:gravity="bottom|left">
  33. 34 <button android:id="@+id/Button01"
  34. 35 android:text="Button"
  35. 36 android:layout_width="wrap_content"
  36. 37 android:layout_height="wrap_content">
  37. 38
    有待研究
下面介绍一下RelativeLayout用到的一些重要的属性:

第一类:属性值为true或false

android:layout_centerHrizontal 水平居中
android:layout_centerVertical 垂直居中
android:layout_centerInparent 相对于父元素完全居中
android:layout_alignParentBottom 贴紧父元素的下边缘
android:layout_alignParentLeft 贴紧父元素的左边缘
android:layout_alignParentRight 贴紧父元素的右边缘
android:layout_alignParentTop 贴紧父元素的上边缘
android:layout_alignWithParentIfMissing 如果对应的兄弟元素找不到的话就以父元素做参照物

第二类:属性值必须为id的引用名“@id/id-name”
android:layout_below 在某元素的下方
android:layout_above 在某元素的的上方
android:layout_toLeftOf 在某元素的左边
android:layout_toRightOf 在某元素的右边

android:layout_alignTop 本元素的上边缘和某元素的的上边缘对齐
android:layout_alignLeft 本元素的左边缘和某元素的的左边缘对齐
android:layout_alignBottom 本元素的下边缘和某元素的的下边缘对齐
android:layout_alignRight 本元素的右边缘和某元素的的右边缘对齐

第三类:属性值为具体的像素值,如30dip,40px
android:layout_marginBottom 离某元素底边缘的距离
android:layout_marginLeft 离某元素左边缘的距离
android:layout_marginRight 离某元素右边缘的距离
android:layout_marginTop 离某元素上边缘的距离
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值