---
title: "Android学习笔记14：相对布局管理器RelativeLayout"
author: "sarin"
date: "2012-12-28T13:25:44Z"
tags: []
categories: ["移动开发"]
slug: "android-14-relativelayout"
draft: false
lastmod: "2012-12-29T12:13:48Z"
id: 1756332
url: "https://sarin.iteye.com/blog/1756332"
---

[接上文](http://sarin.iteye.com/blog/1754910)
    相对布局管理器是基于一个参考点而言的布局管理器。就像Web开发中的相对路径的概念，是基于一定的参考点而创建的。在Android中的相对布局管理器就是在一个参考点的四周（上，下，左，右）布局的管理器。
    下面来看一下RelativeLayout的文档：
![](http://dl.iteye.com/upload/attachment/0078/5307/22ee294e-f1b6-398c-8e93-d69b249ddafe.png)
    它的继承结构为：
**java.lang.Object
   ↳ android.view.View
 	   ↳ android.view.ViewGroup
 	 	   ↳ android.widget.RelativeLayout**
    下面在Eclipse中新建一个项目来看一下相对布局管理器RelativeLayout的使用：

```xml

<?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" >
    <ImageView
        android:id="@+id/img1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />
    <ImageView
        android:id="@+id/img2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/google_plus"
        android:layout_toRightOf="@+id/img1" />
</RelativeLayout>

```

    我们在main.xml中将布局管理器声明为RelativeLayout，之后创建了两个ImageView组件用来显示两幅图片，其中在第二个ImageView组件上设置了layout_toRightOf属性，也就是设置相对于某组件的右侧，这里填入的是组件ID的值，那么这里也就是说我们的img2相对于img1的位置是右侧。下面运行程序，我们看到如下效果：
![](http://dl.iteye.com/upload/attachment/0078/5309/b4849cee-2008-379c-acc1-d059311bb17e.png)
    很明显，第二幅图片放置在了第一副图片的右侧，下面往代码中再加入一个TextView组件：

```xml

    <TextView android:id="@+id/txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="这里是一些显示文字"
        android:layout_below="@+id/img2"/>

```

    这个组件也很简单，我们设置了layout_below属性，说明要放置在第二幅图片的下面，那么运行程序，我们得到如下的显示效果：
![](http://dl.iteye.com/upload/attachment/0078/5311/488c6f4d-dca8-322d-abf9-fadb72ff466a.png)
    没有问题，文字确实在第二幅片的下面了，但是却顶头显示了，如果第一副图片小于第二幅图片，是会产生覆盖效果的，我们调整位置来看一下，调整代码为：

```xml

    <ImageView
        android:id="@+id/img1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
        android:layout_toRightOf="@+id/img2" />
    <ImageView
        android:id="@+id/img2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/google_plus" />
    <TextView android:id="@+id/txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="这里是一些显示文字"
        android:layout_below="@+id/img1"/>

```

    这里不再解释代码的含义，直接运行，我们看到：
![](http://dl.iteye.com/upload/attachment/0078/5313/b527dcb7-6e3d-313a-a538-c780e1bd9640.png)
    文字覆盖第一副图片显示了，那么需要继续对它进行设置：

```xml

    <TextView android:id="@+id/txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="这里是一些显示文字"
        android:layout_below="@+id/img1"
        android:layout_toRightOf="@+id/img2"/>

```

    再次运行程序，我们可以看到如下效果：
![](http://dl.iteye.com/upload/attachment/0078/5315/dbd89cdd-920e-3797-81e3-701056f12e6f.png)
    文字就在img1的下面并且在img2的右侧了。此时文字的下侧和img2的右侧还有一定空间，我们再放置一个Button组件：

```xml

    <Button 
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮"
        android:layout_below="@+id/txt"
        android:layout_toRightOf="@+id/img2"/>

```

    再次运行程序，我们就得到了如下效果：
![](http://dl.iteye.com/upload/attachment/0078/5317/b4335b35-9523-328f-a25b-b77f90a35190.png)
    和其它布局管理器一样，我们可以通过Java代码来实现对相对布局管理器的控制，下面首先来看一下RelativeLayout.LayoutParams的文档：
![](http://dl.iteye.com/upload/attachment/0078/5319/3a461623-9a5f-3805-b3d7-4adb61c342fb.png)
    其继承结构为：
**java.lang.Object
   ↳ android.view.ViewGroup.LayoutParams
 	   ↳ android.view.ViewGroup.MarginLayoutParams
 	 	   ↳ android.widget.RelativeLayout.LayoutParams**
    只是在代码中控制相对布局管理器时需要设置一些规则，也就是我们上面看到的layout_toRightOf和layout_below等，下面来看一下代码：

```java

package org.ourpioneer;
import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RelativeLayout;
public class RelativeLayoutDemoActivity extends Activity {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.main);// 读取已有的布局管理器
		RelativeLayout relativeLayout = (RelativeLayout) super
				.findViewById(R.id.rLayout);// 获取相对布局管理器rLayout
		RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
				ViewGroup.LayoutParams.FILL_PARENT,
				ViewGroup.LayoutParams.FILL_PARENT);// 设置布局管理器参数
		params.addRule(RelativeLayout.BELOW, R.id.btn);// 设置放置规则
		EditText edit = new EditText(this);// 创建EditText组件
		relativeLayout.addView(edit,params);
	}
}

```

    编写代码之前，我们需要在main.xml中为我们的布局管理器添加ID属性，也就是rLayout，之后我们可以在代码中对它进行控制，这里我们在已有的布局管理器之中继续添加组件，也就是要往按钮下放置一个编辑框，那么我们设置布局管理器参数都为FILL_PARENT，就是要填充整个屏幕，然后规则定位在btn的下侧，之后往布局管理器中添加组件，运行程序，我们就可以看到：
![](http://dl.iteye.com/upload/attachment/0078/5321/44490193-c2c4-35de-a9a4-6733891742a6.png)
    代码请参考附件
    [接下文](http://sarin.iteye.com/blog/1756874)