Android五大布局详解

http://www.open-open.com/lib/view/open1390095269414.html

Android 的五大布局分别是LinearLayout(线性布局)、FrameLayout(单帧布局)、RelativeLayout(相对布局)、 AbsoluteLayout(绝对布局)和TableLayout(表格布局),09年的时候,我就该总结啊!分享才有意义!下面分别介绍:

LinearLayout(最好少用):
  LinearLayout按照垂直或者水平的顺序依次排列子元素,每一个子元素都位于前一个元素之后。如果是垂直排列,那么将是一个N行单列的结构,每一行只会有一个元素,而不论这个元素
的宽度为多少;如果是水平排列,那么将是一个单行N列的结构。如果搭建两行两列的结构,通常的方式是先垂直排列两个元素,每一个元素里再包含一个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"/>
           <Button  android:layout_width="fill_parent" 
                   android:layout_height="wrap_content" android:background="#fffedcba" 
                   android:layout_weight="2"  />
    </LinearLayout>
</LinearLayout>


FrameLayout(比较少用):
  FrameLayout是五大布局中最简单的一个布局,在这个布局中,整个界面被当成一块空白备用区域,所有的子元素都不能被指定放置的位置,它们统统放于这块区域的左上角,并且后面的
子元素直接覆盖在前面的子元素之上,将前面的子元素部分和全部遮挡。显示效果如下,第一个TextView被第二个TextView完全遮挡。
<?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"/>
</FrameLayout>


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


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">
    <Button android:id="@+id/bt1" android:layout_width="50dp" 
         android:layout_height="50dp" android:background="#ffffffff" android:gravity="center" 
         android:layout_alignParentBottom="true" />
    <Button android:id="@+id/bt2" android:layout_width="50dp" 
         android:layout_height="50dp" android:background="#ff654321" android:gravity="center" 
         android:layout_above="@id/bt1" android:layout_centerHorizontal="true" />
    <Button android:id="@+id/bt3" android:layout_width="50dp" 
         android:layout_height="50dp" android:background="#fffedcba" android:gravity="center" 
         android:layout_toLeftOf="@id/bt2" android:layout_above="@id/text_01" />
</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中,单元格可以为空,但是不能跨列。

padding是控件的内容相对控件的边缘的边距.
margin是控件边缘相对父控件,或者其他控件的边距

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值