---
title: "布局管理器（二）"
author: "ywChen"
date: "2011-07-28T22:35:26Z"
tags: []
categories: ["移动开发"]
slug: "untitled"
draft: false
lastmod: "2012-02-23T17:01:52Z"
id: 1135206
url: "https://ywchen.iteye.com/blog/1135206"
---

**1.相对布局**

 

    相对布局由RelativeLayout代表，相对布局容器内子组件的位置总是相对兄弟组件、父容器来决定的，因此这种布局方式被称为相对布局。

    如果A组件的位置是由B组件的位置来决定的，Android要求先定义B组件，再顶定义A组件。

 

RelativeLayout的XML属性及相关方法说明

XML属性 相关方法 说明 android:gravity setGravity(int) 设置该布局容器内部各子组件的对齐方式 android:ignoreGravity setIgnoreGravity(int) 设置哪个组件不受gravity组件的影响

 

RelativeLayout.LayoutParams里只能设为boolean值得属性

属性 说明 android:layout\_centerHorizontal 控制该子组件是否位于布局容器的水平居中位置 android:layout\_centerVertical 控制该子组件是否位于布局容器的垂直居中位置 android:layout\_Inparent 控制该子组件是否位于布局容器的中央位置 android:layout\_alignParentBottom 控制该子组件是否位于布局容器低端对齐 android:layout\_alignParentLeft 控制该子组件是否位于布局容器左边对齐

android:layout\_alignParentRight

控制该子组件是否位于布局容器右边对齐 android:layout\_alignParentTop 控制该子组件是否位于布局容器顶端对齐

 

 

RelativeLayout.LayoutParams里只能设为其他UI组件ID的属性

 

XML属性 说明 android:layout\_toRightOf 控制该子组件位于给出ID组件的右侧 android:layout\_toLeftOf 控制该子组件位于给出ID组件的左侧 android:layout\_above 控制该子组件位于给出ID组件的上方 android:layout\_below 控制该子组件位于给出ID组件的下方 android:layout\_alignTop 控制该子组件位于给出ID组件的上边界对齐 android:layout\_alignBottom 控制该子组件位于给出ID组件的下边界对齐 android:layout\_alignLeft 控制该子组件位于给出ID组件的左边界对齐 android:layout\_alignRight 控制该子组件位于给出ID组件的右边界对齐

 

展示梅花布局效果

 

```
<?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:layout_height="wrap_content" 
			android:layout_width="wrap_content"
			android:id="@+id/rview01"
			android:layout_centerInParent="true" 
			android:background="@drawable/leaf">
	</TextView>
	<!-- 定义该组件位于rview01组件的上方 -->
	<TextView android:layout_height="wrap_content" 
			android:layout_width="wrap_content"
			android:id="@+id/rview02"
			android:background="@drawable/leaf"
		    android:layout_above="@+id/rview01"
		    android:layout_alignLeft="@+id/rview01">
	</TextView>
	<!-- 定义该组件位于rview01组件的下方 -->
	<TextView android:layout_height="wrap_content" 
			android:layout_width="wrap_content"
			android:id="@+id/rview03"
			android:background="@drawable/leaf"
		    android:layout_below="@+id/rview01"
		    android:layout_alignLeft="@+id/rview01">
	</TextView>
	<!-- 定义该组件位于rview01组件的左边 -->
	<TextView android:layout_height="wrap_content" 
			android:layout_width="wrap_content"
			android:id="@+id/rview04"
			android:background="@drawable/leaf"
		    android:layout_toLeftOf="@+id/rview01"
		    android:layout_alignTop="@+id/rview01" >
	</TextView>
	<!-- 定义该组件位于rview01组件的右边 -->
	<TextView android:layout_height="wrap_content" 
			android:layout_width="wrap_content"
			android:id="@+id/rview05"
			android:background="@drawable/leaf"
		    android:layout_toRightOf="@+id/rview01"
		    android:layout_alignTop="@+id/rview01">
	</TextView>
</RelativeLayout>
```

 效果图：

 

![](http://dl.iteye.com/upload/attachment/526322/70bc3f75-bedd-3a63-82b4-86187cb4db35.jpg)

 

**2.绝对布局**

 

    绝对布局由AbsoluteLayout代表，Android不提供如何布局控制，而是由开发人员自己通过X坐标，Y坐标来控制组件的位置。

 

XML属性说明

 

XML属性 说明 android:layout\_x 指定该子组件的X坐标 android:layout\_y 指定该子组件的Y坐标

 

Android中一般支持以下常用的距离单位

**px（像素）**：每个px对应屏幕上的一个点。

**dip或dp（device independent pixels，设备独立像素）**：一种及与屏幕密度的抽象单位。 在每英寸160点的显示器上，1dip=1px，但随着屏幕密度的改变，dip与px的换算会发生改变。

**sp（scaled pixels，比例像素）**：主要处理字体的大小，可以根据用户的字体大小首选项进行缩放

**in(英寸)**：标准长度单位

**mm(毫米)**：标准长度单位

**pt（磅）**：标准长度单位，1/72英寸

 

登录界面

 

```
<?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_height="wrap_content" 
			android:layout_width="wrap_content"
			android:layout_x="20dip"
			android:layout_y="20dip"
			android:text="用户名：">
	</TextView>
	<EditText android:layout_height="wrap_content" 
			android:layout_width="wrap_content"
			android:layout_x="80dip"
			android:layout_y="15dip"
			android:width="200px">
	</EditText>
	<TextView android:layout_height="wrap_content" 
			android:layout_width="wrap_content"
			android:layout_x="20dip"
			android:layout_y="80dip"
			android:text="密码：">
	</TextView>
	<EditText android:layout_height="wrap_content" 
			android:layout_width="wrap_content"
			android:layout_x="80dip"
			android:layout_y="75dip"
			android:password="true"
			android:width="200px">
	</EditText>
	<Button android:layout_x="130dip"
			android:layout_y="135dip"
			android:layout_height="wrap_content" 
			android:layout_width="wrap_content"
			android:text="登录"/>
</AbsoluteLayout>
```

 

效果图  
![](http://dl.iteye.com/upload/attachment/526327/57e05c7e-4046-3006-b7b9-a062341d7cd4.jpg)

 

**本人开了个充值淘宝网店。有需要的朋友请访问的店铺并拍下所充值的话费，**

**本店已加入****消费保障服务计划，货源来源于淘宝充值平台，安全可靠便捷，**

**支付过后立即到账**

![](http://dl.iteye.com/upload/attachment/0063/6535/7f84c3eb-bdd7-3220-b298-50f01ed07911.jpg)

[http://xiaowen168.taobao.com](http://xiaowen168.taobao.com/ "http://xiaowen168.taobao.com")