免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1544 | 回复: 5
打印 上一主题 下一主题

[Android] android五种布局模式 [复制链接]

论坛徽章:
80
20周年集字徽章-庆
日期:2020-10-28 14:09:1215-16赛季CBA联赛之北京
日期:2020-10-28 13:32:5315-16赛季CBA联赛之北控
日期:2020-10-28 13:32:4815-16赛季CBA联赛之天津
日期:2020-10-28 13:13:35黑曼巴
日期:2020-10-28 12:29:1520周年集字徽章-周	
日期:2020-10-31 15:10:0720周年集字徽章-20	
日期:2020-10-31 15:10:07ChinaUnix元老
日期:2015-09-29 11:56:3020周年集字徽章-年
日期:2020-10-28 14:14:56
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-09-23 20:46 |只看该作者 |倒序浏览
Android布局是应用界面开发的重要一环,在Android中,共有五种布局方式,分别是:LinearLayout (线性布局),FrameLayout(框架布
局),AbsoluteLayout(绝对布局),RelativeLayout(相对布局),TableLayout(表格布局)。

在windows下有预览功能,可以在xml中查看布局的样式,在linux中无。

一、LinearLayout
      线性布局,这个东西,从外框上可以理解为一个div,他首先是一个一个从上往下罗列在屏幕上。每一个LinearLayout里面又可分为垂直布局(androidrientation="vertical")和水平布局(androidrientation="horizontal" )。当垂直布局时,每一行就只有一个元素,多个元素依次垂直往下;水平布局时,只有一行,每一个元素依次向右排列。
  linearLayout中有一个重要的属性 android:layout_weight="1",这个weight在垂直布局时,代表行距;水平的时候代表列宽;weight值越大就越大。

线形布局中预览和真机中完全一样。

TextView占一定的空间,没有赋值也有一定的宽高,要特别注意。
二、FrameLayout
      FrameLayout是最简单的一个布局对象。它被定制为你屏幕上的一个空白备用区域,之后你可以在其中填充一个单一对象 — 比如,一张你要发布的图片。所有的子元素将会固定在屏幕的左上角;你不能为FrameLayout中的一个子元素指定一个位置。后一个子元素将会直接在前一个子元素之上进行覆盖填充,把它们部份或全部挡住(除非后一个子元素是透明的)。   
三、AbsoluteLayout
   AbsoluteLayout 这个布局方式很简单,主要属性就两个 layout_x 和 layout_y 分别定义 这个组件的绝对位置。 即,以屏幕左上角为(0,0)的坐标轴的x,y值,当向下或向右移动时,坐标值将变大。AbsoluteLayout 没有页边框,允许元素之间互相重叠(尽管不推荐)。我们通常不推荐使用 AbsoluteLayout ,除非你有正当理由要使用它,因为它使界面代码太过刚性,以至于在不同的设备上可能不能很好地工作。

四、RelativeLayout
    相对布局可以理解为某一个元素为参照物,来定位的布局方式。

                android:layout_方向 = id  表示 在这个id对应的控件的方向上(上|下)

                android:layout_align方向  = id 表示和这个控件的(上下左右)对齐

                android: layout_to方向Of  = id 表示在这个控件的 左或者右
eg:

                  android:layout_below="@id/la1"/>
                将当前控件放置于id为la1 的控件下方。
                         android:layout_alignParentRight="true"
                使当前控件的右端和父控件的右端对齐。这里属性值只能为true或false,默认false。
                 android:layout_marginLeft="10dip"
                使当前控件左边空出相应的空间。
                         android:layout_toLeftOf="@id/true"
                使当前控件置于id为true的控件的左边。
                         android:layout_alignTop="@id/ok"
                使当前控件与id为ok的控件上端对齐。

        五、TableLayout

       表格布局类似Html里面的Table。每一个TableLayout里面有表格行TableRow,TableRow里面可以具体定义每一个元素。每个TableRow 都会定义一个 row (事实上,你可以定义其它的子对象,这在下面会解释到)。TableLayout 容器不会显示row 、cloumns 或cell 的边框线。每个 row 拥有0个或多个的cell ;每个cell 拥有一个View 对象。表格由列和行组成许多的单元格。表格允许单元格为空。单元格不能跨列,这与HTML 中的不一样。

TabRow只论行,不论列(列自定义)。

  每一个布局都有自己适合的方式,另外,这五个布局元素可以相互嵌套应用,做出美观的界面。

例子:


1 线性布局(LinearLayout)


描述:最简单布局方式,依次向下进行排列。
  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.     <Button android:text="Up"
  7.         android:id="@+id/Button03"
  8.         android:layout_width="fill_parent"
  9.         android:layout_height="wrap_content"></Button>
  10.         
  11.     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  12.         android:orientation="horizontal"
  13.         android:layout_width="fill_parent"
  14.         android:layout_height="fill_parent">        
  15.     <Button android:text="left"
  16.         android:id="@+id/Button01"
  17.         android:width="120px"
  18.         android:layout_width="wrap_content"
  19.         android:layout_height="wrap_content"></Button>   
  20.     <Button
  21.         android:text="right"
  22.         android:id="@+id/Button02"
  23.         android:width="120px"
  24.         android:layout_width="wrap_content"
  25.         android:layout_height="wrap_content"></Button>
  26.     </LinearLayout>
  27. </LinearLayout>
复制代码

论坛徽章:
80
20周年集字徽章-庆
日期:2020-10-28 14:09:1215-16赛季CBA联赛之北京
日期:2020-10-28 13:32:5315-16赛季CBA联赛之北控
日期:2020-10-28 13:32:4815-16赛季CBA联赛之天津
日期:2020-10-28 13:13:35黑曼巴
日期:2020-10-28 12:29:1520周年集字徽章-周	
日期:2020-10-31 15:10:0720周年集字徽章-20	
日期:2020-10-31 15:10:07ChinaUnix元老
日期:2015-09-29 11:56:3020周年集字徽章-年
日期:2020-10-28 14:14:56
2 [报告]
发表于 2015-09-23 20:47 |只看该作者
2、 表格布局(TableLayout)


描述:类似于HTML table ,在其中间添加View 或是<TableRow></TableRow>控件。
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TableLayout
  3.     xmlns:android="http://schemas.android.com/apk/res/android"
  4.     android:id="@+id/TableLayout01"
  5.     android:layout_width="fill_parent" android:layout_height="fill_parent">
  6.     <TableRow android:gravity="center">
  7.       <Button
  8.             android:text="@+id/Button01"
  9.             android:id="@+id/Button01"
  10.             android:layout_width="wrap_content"
  11.             android:layout_height="wrap_content">
  12.        </Button>
  13.     </TableRow>
  14.     <TableRow android:gravity="center">
  15.         <TextView android:text="第一行第0列"
  16.                   android:layout_width="wrap_content"
  17.                   android:layout_height="wrap_content"></TextView>
  18.         <TextView android:text="第一行第1列"
  19.                   android:layout_width="wrap_content"
  20.                   android:layout_height="wrap_content"></TextView>
  21.     </TableRow>
  22.      <TableRow android:gravity="center">
  23.         <TextView android:text="第二行第0列"
  24.                   android:layout_width="wrap_content"
  25.                   android:layout_height="wrap_content"></TextView>
  26.         <TextView android:text="第二行第1列"
  27.                   android:layout_width="wrap_content"
  28.                   android:layout_height="wrap_content"></TextView>
  29.     </TableRow>
  30. </TableLayout>
复制代码

论坛徽章:
80
20周年集字徽章-庆
日期:2020-10-28 14:09:1215-16赛季CBA联赛之北京
日期:2020-10-28 13:32:5315-16赛季CBA联赛之北控
日期:2020-10-28 13:32:4815-16赛季CBA联赛之天津
日期:2020-10-28 13:13:35黑曼巴
日期:2020-10-28 12:29:1520周年集字徽章-周	
日期:2020-10-31 15:10:0720周年集字徽章-20	
日期:2020-10-31 15:10:07ChinaUnix元老
日期:2015-09-29 11:56:3020周年集字徽章-年
日期:2020-10-28 14:14:56
3 [报告]
发表于 2015-09-23 20:47 |只看该作者
3、 单帧布局(FrameLayout)


描述:类似于HTML层叠
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <FrameLayout
  3.     xmlns:android="http://schemas.android.com/apk/res/android"
  4.     android:layout_width="wrap_content"
  5.     android:layout_height="wrap_content">
  6.     <ImageView
  7.         android:id="@+id/ImageView01"
  8.         android:src="@drawable/circle_blue"
  9.         android:layout_width="wrap_content"
  10.         android:layout_height="wrap_content">
  11.     </ImageView>
  12.     <ImageView
  13.         android:id="@+id/ImageView02"
  14.         android:src="@drawable/circle_green"
  15.         android:layout_width="wrap_content"
  16.         android:layout_height="wrap_content">
  17.     </ImageView>
  18.     <ImageView
  19.         android:id="@+id/ImageView03"
  20.         android:src="@drawable/circle_red"
  21.         android:layout_width="wrap_content"
  22.         android:layout_height="wrap_content">
  23.     </ImageView>
  24.    
  25. </FrameLayout>
复制代码

论坛徽章:
80
20周年集字徽章-庆
日期:2020-10-28 14:09:1215-16赛季CBA联赛之北京
日期:2020-10-28 13:32:5315-16赛季CBA联赛之北控
日期:2020-10-28 13:32:4815-16赛季CBA联赛之天津
日期:2020-10-28 13:13:35黑曼巴
日期:2020-10-28 12:29:1520周年集字徽章-周	
日期:2020-10-31 15:10:0720周年集字徽章-20	
日期:2020-10-31 15:10:07ChinaUnix元老
日期:2015-09-29 11:56:3020周年集字徽章-年
日期:2020-10-28 14:14:56
4 [报告]
发表于 2015-09-23 20:48 |只看该作者
4、 相对布局(RelativeLayout)


描述:取决于对参照控件进行布局,父控件和子控件均可

常用属性:android:layout_centerInParent=”true/false”        

          android:layout_above, android:layout_below

    android:layout_alignleft, android:layout_alignright.
  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" android:layout_height="fill_parent">
  4.      <Button
  5.           android:id="@+id/btnmiddle"
  6.           android:text="MiddleButton"
  7.           android:layout_width="200px"
  8.           android:layout_height="wrap_content"
  9.           android:layout_centerInParent="true">     
  10.      </Button>
  11.       <Button
  12.           android:id="@+id/btnup"
  13.           android:text="UpButton"
  14.           android:layout_width="100px"
  15.           android:layout_height="wrap_content"         
  16.           android:layout_above="@id/btnmiddle"
  17.           android:layout_alignLeft="@id/btnmiddle">     
  18.      </Button>
  19.       <Button
  20.           android:id="@+id/btndown"
  21.           android:text="downButton"
  22.           android:layout_width="100px"
  23.           android:layout_height="wrap_content"         
  24.           android:layout_below="@id/btnmiddle"
  25.           android:layout_alignRight="@id/btnmiddle">     
  26.      </Button>
  27. </RelativeLayout>
复制代码

论坛徽章:
80
20周年集字徽章-庆
日期:2020-10-28 14:09:1215-16赛季CBA联赛之北京
日期:2020-10-28 13:32:5315-16赛季CBA联赛之北控
日期:2020-10-28 13:32:4815-16赛季CBA联赛之天津
日期:2020-10-28 13:13:35黑曼巴
日期:2020-10-28 12:29:1520周年集字徽章-周	
日期:2020-10-31 15:10:0720周年集字徽章-20	
日期:2020-10-31 15:10:07ChinaUnix元老
日期:2015-09-29 11:56:3020周年集字徽章-年
日期:2020-10-28 14:14:56
5 [报告]
发表于 2015-09-23 20:48 |只看该作者
5、 坐标布局(AbsoluteLayout)



描述:对其控件进行直接定位,增加灵活性。

常用属性:android:layout_x,android:layout_y.
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <AbsoluteLayout
  3.   xmlns:android="http://schemas.android.com/apk/res/android"
  4.   android:layout_width="wrap_content"
  5.   android:layout_height="wrap_content">
  6.   
  7.   <TextView
  8.        android:layout_width="wrap_content"
  9.        android:text="UserName:"
  10.        android:layout_height="wrap_content"
  11.        android:id="@+id/tvName"
  12.        android:layout_y="20dip"
  13.        android:layout_x="50dip">
  14.        </TextView>
  15.    <TextView
  16.        android:layout_width="wrap_content"
  17.        android:text="Password:"
  18.        android:layout_height="wrap_content"
  19.        android:id="@+id/tvPassword"
  20.        android:layout_y="100dip"
  21.        android:layout_x="55dip">
  22.        </TextView>
  23.       
  24.    <EditText
  25.        android:layout_width="150px"
  26.        android:layout_height="wrap_content"
  27.        android:id="@+id/tvPassword"
  28.        android:layout_y="10dip"
  29.        android:layout_x="120dip">
  30.        </EditText>
  31.    <EditText
  32.        android:layout_width="150px"
  33.        android:layout_height="wrap_content"
  34.        android:id="@+id/tvPassword"
  35.        android:layout_y="90dip"
  36.        android:layout_x="120dip">
  37.        </EditText>
  38. </AbsoluteLayout>
复制代码

论坛徽章:
80
20周年集字徽章-庆
日期:2020-10-28 14:09:1215-16赛季CBA联赛之北京
日期:2020-10-28 13:32:5315-16赛季CBA联赛之北控
日期:2020-10-28 13:32:4815-16赛季CBA联赛之天津
日期:2020-10-28 13:13:35黑曼巴
日期:2020-10-28 12:29:1520周年集字徽章-周	
日期:2020-10-31 15:10:0720周年集字徽章-20	
日期:2020-10-31 15:10:07ChinaUnix元老
日期:2015-09-29 11:56:3020周年集字徽章-年
日期:2020-10-28 14:14:56
6 [报告]
发表于 2015-09-23 20:52 |只看该作者
建议大家使用相对布局,首先它的方法属性是最强大的其次它基本可以实现其它4大布局的效果,当然这里说的不是全部 有时候还是须要使用其他布局
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP