绝对布局由AbsoluteLayout代表。绝对布局就像java AWT编程中的空布局,就是Android不提供任何布局控制而是由开发人员自己通过X坐标、Y坐标来控制组件的位置。当使用AbsoluteLayout作为布局容器时,布局容器不再管理子组件的位置、大小---这些都需要开发人员自己控制。
使用绝对布局是,每个组件都可指定如下两个XML属性
layout_x:指定该组件的X坐标
layout_y:指定该组件的Y坐标
例子:登录界面
main.xml代码
01 <?xml version="1.0" encoding="utf-8"?>
02 <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
03 android:orientation="vertical"
04 android:layout_width="fill_parent"
05 android:layout_height="fill_parent"
06 >
07
08 <!-- 定义一个文本框,使用绝对定位 -->
09 <TextView
10 android:layout_x="20dip"
11 android:layout_y="20dip"
12 android:layout_width="wrap_content"
13 android:layout_height="wrap_content"
14 android:text="用户名:"/>
15 <!-- 定义一个文本编辑框,使用绝对定位 -->
16 <EditText
17 android:layout_x="80dip"
18 android:layout_y="15dip"
19 android:layout_width="wrap_content"
20 android:layout_height="wrap_content"
21 android:width="200px"/>
22 <!-- 定义一个文本框,使用绝对定位 -->
23 <TextView
24 android:layout_x="20dip"
25 android:layout_y="80dip"
26 android:layout_width="wrap_content"
27 android:layout_height="wrap_content"
28 android:text="密码:"/>
29 <!-- 定义一个文本编辑框,使用绝对定位 -->
30 <EditText
31 android:layout_x="80dip"
32 android:layout_y="75dip"
33 android:layout_width="wrap_content"
34 android:layout_height="wrap_content"
35 android:width="200px"
36 android:password="true"/>
37 <!-- 定义一个按钮,使用绝对定位 -->
38 <Button
39 android:layout_x="130dip"
40 android:layout_y="135dip"
41 android:layout_width="wrap_content"
42 android:layout_height="wrap_content"
43 android:text="登录"/>
44 </AbsoluteLayout>
java 代码:
view sourceprint?
01 package com.nuaa.absolutelayout;
02
03 import android.app.Activity;
04 import android.os.Bundle;
05
06 public class AbsoluteLayoutTest extends Activity {
07 public void onCreate(Bundle savedInstanceState){
08 super.onCreate(savedInstanceState);
09 setContentView(R.layout.main);
10 }
11
12 }