Android五大布局(二)——RelativeLayout、TableLayout 和AbsoulteLayout

在 Android五大布局(一)[url]http://dtfy56924.iteye.com/blog/1775551[/url] 中已经描述了LinearLayout(线性布局)、FrameLayout(单帧布局)两种布局结构
下面说下RelativeLayout、TableLayout和AbsoulteLayout

[b]Android 众多的布局属性详解[/b]
[url]http://www.open-open.com/lib/view/open1328686184311.html[/url]
[b]参考与:[/b]
[url]http://www.eoeandroid.com/thread-249585-1-1.html[/url]

[color=red][b]一、RelativeLayout:[/b][/color]
  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五大布局结构中最灵活的一种布局结构,比较适合一些复杂界面的布局。下面示例就展示这么一个情况,第一个文本框与父组件的底部对齐,第二个文本框位于第一个文本框的上方,并且第三个文本框位于第二个文本框的左方。

[b]例子效果图:[/b]
[img]http://dl.iteye.com/upload/attachment/0079/8356/e8b82d56-c93e-3ebd-82d4-bbb20fbab851.jpg[/img]

<?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"
android:orientation="vertical" >

<TextView
android:id="@+id/text_01"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="#ffffffff"
android:gravity="center"
android:text="1" />

<TextView
android:id="@+id/text_02"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_above="@id/text_01"
android:layout_centerHorizontal="true"
android:background="#ff654321"
android:gravity="center"
android:text="2" />

<TextView
android:id="@+id/text_03"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_above="@id/text_01"
android:layout_toLeftOf="@id/text_02"
android:background="#fffedcba"
android:gravity="center"
android:text="3" />
</RelativeLayout>


[b][color=red]二、TableLayout:[/color][/b]
TableLayout顾名思义,此布局为表格布局,适用于N行N列的布局格式。一个TableLayout由许多TableRow组成,一个TableRow就代表TableLayout中的一行。
TableRow里面可以具体定义每一个元素,设定他的对齐方式 android:gravity="" 。
TableRow是LinearLayout的子类,它的android:orientation属性值恒为horizontal,并且它的android:layout_width和android:layout_height属性值恒为MATCH_PARENT和WRAP_CONTENT。所以它的子元素都是横向排列,并且宽高一致的。这样的设计使得每个TableRow里的子元素都相当于表格中的单元格一样。在TableRow中,单元格可以为空,但是不能跨列。
下面示例演示了一个TableLayout的布局结构,其中第二行只有两个单元格,而其余行都是三个单元格。

[b]例子效果图:[/b]

[img]http://dl.iteye.com/upload/attachment/0079/8358/3b148071-42a9-3876-b221-1984ee3a172c.jpg[/img]

<?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:orientation="vertical" >

<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>


[color=red][b]三、AbsoluteLayout:[/b][/color]
  AbsoluteLayout是绝对位置布局。在此布局中的子元素的android:layout_x和android:layout_y属性将生效,用于描述该子元素的坐标位置。屏幕左上角为坐标原点(0,0),第一个0代表横坐标,向右移动此值增大,第二个0代表纵坐标,向下移动,此值增大。在此布局中的子元素可以相互重叠。在实际开发中,通常不采用此布局格式,因为它的界面代码过于刚性,以至于有可能不能很好的适配各种终端。
在垂直随便切换时,往往会出问题,而且多个元素的时候,计算比较麻烦。显示效果如下:

[b]例子效果图:[/b]
[img]http://dl.iteye.com/upload/attachment/0079/8360/f8c243d9-fa33-3d6d-9f70-84f703699c15.jpg[/img]


<?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"
android:orientation="vertical" >

<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_x="50dp"
android:layout_y="50dp"
android:background="#ffffffff"
android:gravity="center"
android:text="1" />

<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_x="25dp"
android:layout_y="25dp"
android:background="#ff654321"
android:gravity="center"
android:text="2" />

<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_x="125dp"
android:layout_y="125dp"
android:background="#fffedcba"
android:gravity="center"
android:text="3" />
</AbsoluteLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,Android系统常用的五种布局方式分别是: 1. 线性布局(LinearLayout):线性布局是一种非常常见的布局方式,它按照水平或者垂直方向排列控件,每个控件可以通过权重属性来占据剩余空间。线性布局简单易用,但是不能灵活地适应不同的屏幕大小和分辨率。 2. 相对布局RelativeLayout):相对布局是根据控件之间的相对位置来排列控件的,它可以根据需要调整控件的位置和大小,适应不同的屏幕大小和分辨率。但是相对布局的层级较多时,性能会受到影响。 3. 帧布局(FrameLayout):帧布局是将控件按照层叠顺序排列在一起的布局方式,每个控件占据整个布局的全部空间。帧布局适用于需要在同一个位置上显示多个控件的情况,也可以用来实现简单的动画效果。 4. 表格布局TableLayout):表格布局是将控件按照行列的方式排列在一起的布局方式,每个控件占据一个单元格。表格布局适用于需要显示大量数据的情况,但是在屏幕较小的设备上可能会导致单元格过小而无法显示完整。 5. 网格布局(GridLayout):网格布局是将控件按照网格的方式排列在一起的布局方式,每个控件占据一个单元格,可以通过设置权重属性来占据多个单元格。网格布局适用于需要显示复杂结构的情况,但是在屏幕较小的设备上可能会导致单元格过小而无法显示完整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值