当前位置: 移动技术网 > IT编程>移动开发>Android > 简析Android五大布局(LinearLayout、FrameLayout、RelativeLayout等)

简析Android五大布局(LinearLayout、FrameLayout、RelativeLayout等)

2019年07月24日  | 移动技术网IT编程  | 我要评论

道道通地图免费升级,金琥,寮步论坛

android的界面是有布局和组件协同完成的,布局好比是建筑里的框架,而组件则相当于建筑里的砖瓦。组件按照布局的要求依次排列,就组成了用户所看见的界面。android的五大布局分别是linearlayout(线性布局)、framelayout(单帧布局)、relativelayout(相对布局)、absolutelayout(绝对布局)和tablelayout(表格布局)。

布局一:linearlayout

linearlayout按照垂直或者水平的顺序依次排列子元素,每一个子元素都位于前一个元素之后。如果是垂直排列,那么将是一个n行单列的结构,每一行只会有一个元素,而不论这个元素的宽度为多少;如果是水平排列,那么将是一个单行n列的结构。如果搭建两行两列的结构,通常的方式是先垂直排列两个元素,每一个元素里再包含一个linearlayout进行水平排列。

linearlayout中的子元素属性android:layout_weight生效,它用于描述该子元素在剩余空间中占有的大小比例。加入一行只有一个文本框,那么它的默认值就为0,如果一行中有两个等长的文本框,那么他们的android:layout_weight值可以是同为1。如果一行中有两个不等长的文本框,那么他们的android:layout_weight值分别为1和2,那么第一个文本框将占据剩余空间的三分之二,第二个文本框将占据剩余空间中的三分之一。android:layout_weight遵循数值越小,重要度越高的原则。显示效果如下:

linearlayout 
 <?xml version="1.0" encoding="utf-8"?> 
 <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> 
 <textview android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#ff000000" android:text="@string/hello"/> 
 <linearlayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent"> 
  <textview android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#ff654321" android:layout_weight="1" android:text="1"/> 
  <textview android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#fffedcba" android:layout_weight="2" android:text="2"/> 
 </linearlayout> 
 </linearlayout> 

布局二:framelayout

framelayout是五大布局中最简单的一个布局,在这个布局中,整个界面被当成一块空白备用区域,所有的子元素都不能被指定放置的位置,它们统统放于这块区域的左上角,并且后面的子元素直接覆盖在前面的子元素之上,将前面的子元素部分和全部遮挡。显示效果如下,第一个textview被第二个textview完全遮挡,第三个textview遮挡了第二个textview的部分位置。

framelayout
 <?xml version="1.0" encoding="utf-8"?> 
 <framelayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> 
 <textview android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ff000000" android:gravity="center" android:text="1"/> 
 <textview android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ff654321" android:gravity="center" android:text="2"/> 
 <textview android:layout_width="50dp" android:layout_height="50dp" android:background="#fffedcba" android:gravity="center" android:text="3"/> 
 </framelayout> 

布局三:absolutelayout

absolutelayout是绝对位置布局。在此布局中的子元素的android:layout_x和android:layout_y属性将生效,用于描述该子元素的坐标位置。屏幕左上角为坐标原点(0,0),第一个0代表横坐标,向右移动此值增大,第二个0代表纵坐标,向下移动,此值增大。在此布局中的子元素可以相互重叠。在实际开发中,通常不采用此布局格式,因为它的界面代码过于刚性,以至于有可能不能很好的适配各种终端。显示效果如下:

absolutelayout 
 <?xml version="1.0" encoding="utf-8"?> 
 <absolutelayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> 
 <textview android:layout_width="50dp" android:layout_height="50dp" android:background="#ffffffff" android:gravity="center" android:layout_x="50dp" android:layout_y="50dp" android:text="1"/> 
 <textview android:layout_width="50dp" android:layout_height="50dp" android:background="#ff654321" android:gravity="center" android:layout_x="25dp" android:layout_y="25dp" android:text="2"/> 
 <textview android:layout_width="50dp" android:layout_height="50dp" android:background="#fffedcba" android:gravity="center" android:layout_x="125dp" android:layout_y="125dp" android:text="3"/> 
 </absolutelayout> 

布局四:relativelayout

relativelayout按照各子元素之间的位置关系完成布局。在此布局中的子元素里与位置相关的属性将生效。例如android:layout_below, android:layout_above等。子元素就通过这些属性和各自的id配合指定位置关系。注意在指定位置关系时,引用的id必须在引用之前,先被定义,否则将出现异常。

relativelayout里常用的位置属性如下:
    android:layout_toleftof —— 该组件位于引用组件的左方
    android:layout_torightof —— 该组件位于引用组件的右方
    android:layout_above —— 该组件位于引用组件的上方
    android:layout_below —— 该组件位于引用组件的下方
          android:layout_alignparentleft —— 该组件是否对齐父组件的左端
          android:layout_alignparentright —— 该组件是否齐其父组件的右端
          android:layout_alignparenttop —— 该组件是否对齐父组件的顶部
          android:layout_alignparentbottom —— 该组件是否对齐父组件的底部
    android:layout_centerinparent —— 该组件是否相对于父组件居中
    android:layout_centerhorizontal —— 该组件是否横向居中
    android:layout_centervertical —— 该组件是否垂直居中

relativelayout是android五大布局结构中最灵活的一种布局结构,比较适合一些复杂界面的布局。下面示例就展示这么一个情况,第一个文本框与父组件的底部对齐,第二个文本框位于第一个文本框的上方,并且第三个文本框位于第二个文本框的左方。

relativelayout 
 <?xml version="1.0" encoding="utf-8"?> 
 <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> 
 <textview android:id="@+id/text_01" android:layout_width="50dp" android:layout_height="50dp" android:background="#ffffffff" android:gravity="center" android:layout_alignparentbottom="true" android:text="1"/> 
 <textview android:id="@+id/text_02" android:layout_width="50dp" android:layout_height="50dp" android:background="#ff654321" android:gravity="center" android:layout_above="@id/text_01" android:layout_centerhorizontal="true" android:text="2"/> 
 <textview android:id="@+id/text_03" android:layout_width="50dp" android:layout_height="50dp" android:background="#fffedcba" android:gravity="center" android:layout_toleftof="@id/text_02" android:layout_above="@id/text_01" android:text="3"/> 
 </relativelayout> 

布局五:tablelayout

tablelayout顾名思义,此布局为表格布局,适用于n行n列的布局格式。一个tablelayout由许多tablerow组成,一个tablerow就代表tablelayout中的一行。

tablerow是linearlayout的子类,它的android:orientation属性值恒为horizontal,并且它的android:layout_width和android:layout_height属性值恒为match_parent和wrap_content。所以它的子元素都是横向排列,并且宽高一致的。这样的设计使得每个tablerow里的子元素都相当于表格中的单元格一样。在tablerow中,单元格可以为空,但是不能跨列。

下面示例演示了一个tablelayout的布局结构,其中第二行只有两个单元格,而其余行都是三个单元格。

tablelayout 
 <?xml version="1.0" encoding="utf-8"?>
 <tablelayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
 <tablerow android:layout_width="fill_parent" android:layout_height="wrap_content">
  <textview android:background="#ffffffff" android:gravity="center" android:padding="10dp" android:text="1"/>
  <textview android:background="#ff654321" android:gravity="center" android:padding="10dp" android:text="2"/>
  <textview android:background="#fffedcba" android:gravity="center" android:padding="10dp" android:text="3"/>
 </tablerow>
 <tablerow android:layout_width="fill_parent" android:layout_height="wrap_content">
  <textview android:background="#ff654321" android:gravity="center" android:padding="10dp" android:text="2"/>
  <textview android:background="#fffedcba" android:gravity="center" android:padding="10dp" android:text="3"/> 
 </tablerow>
 <tablerow android:layout_width="fill_parent" android:layout_height="wrap_content">
  <textview android:background="#fffedcba" android:gravity="center" android:padding="10dp" android:text="3"/>
  <textview android:background="#ff654321" android:gravity="center" android:padding="10dp" android:text="2"/>
  <textview android:background="#ffffffff" android:gravity="center" android:padding="10dp" android:text="1"/> 
 </tablerow>
 </tablelayout>

以上就是本文的全部内容,希望对大家学习android五大布局有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网