作者:刘昊昱

博客:http://blog.csdn.net/liuhaoyutz

相对布局RelativeLayout是指按照组件之间的相对位置进行布局,如一个组件在另一个组件的左边、右边、上边或下边等。

RelativeLayout常用的XML属性有:

android:gravity

用于设置布局管理器中各子组件的对齐方式。

android:ignoreGravity

用于指定哪个组件不受gravity属性的影响。

在相对布局管理器中,如果只有上面两个属性是远远不够的,为了更好地控制该布局管理器中的组件,RelativeLayout提供了一个内部类:RelativeLayout.LayoutParams,通过该类提供的大量XML属性,可以很好的控制RelativeLayout中的各组件的布局。

下表是Android官方网站上给出的RelativeLayout.LayoutParams的XML属性

Attribute Name

Description

android:layout_above

Positions the bottom edge of this view above the given anchor view ID.

android:layout_alignBaseline

Positions the baseline of this view on the baseline of the given anchor view ID.

android:layout_alignBottom

Makes the bottom edge of this view match the bottom edge of the given anchor view ID.

android:layout_alignEnd

Makes the end edge of this view match the end edge of the given anchor view ID.

android:layout_alignLeft

Makes the left edge of this view match the left edge of the given anchor view ID.

android:layout_alignParentBottom

If true, makes the bottom edge of this view match the bottom edge of the parent.

android:layout_alignParentEnd

If true, makes the end edge of this view match the end edge of the parent.

android:layout_alignParentLeft

If true, makes the left edge of this view match the left edge of the parent.

android:layout_alignParentRight

If true, makes the right edge of this view match the right edge of the parent.

android:layout_alignParentStart

If true, makes the start edge of this view match the start edge of the parent.

android:layout_alignParentTop

If true, makes the top edge of this view match the top edge of the parent.

android:layout_alignRight

Makes the right edge of this view match the right edge of the given anchor view ID.

android:layout_alignStart

Makes the start edge of this view match the start edge of the given anchor view ID.

android:layout_alignTop

Makes the top edge of this view match the top edge of the given anchor view ID.

android:layout_alignWithParentIfMissing

If set to true, the parent will be used as the anchor when the anchor cannot be be found for layout_toLeftOf, layout_toRightOf, etc.

android:layout_below

Positions the top edge of this view below the given anchor view ID.

android:layout_centerHorizontal

If true, centers this child horizontally within its parent.

android:layout_centerInParent

If true, centers this child horizontally and vertically within its parent.

android:layout_centerVertical

If true, centers this child vertically within its parent.

android:layout_toEndOf

Positions the start edge of this view to the end of the given anchor view ID.

android:layout_toLeftOf

Positions the right edge of this view to the left of the given anchor view ID.

android:layout_toRightOf

Positions the left edge of this view to the right of the given anchor view ID.

android:layout_toStartOf

Positions the end edge of this view to the start of the given anchor view ID.

下面我们来看一个例子,该程序运行效果如图所示:

我们来看该程序的主布局文件main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<!-- 添加一个居中显示的文本视图textView1 -->
<TextView android:text="您正在学习RelativeLayout的用法 ,是否开始动手练习?"
android:id="@+id/textView1"
android:textSize="24px"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerInParent="true"
/>
<!-- 添加一个在button2左侧显示的按钮button1 -->
<Button
android:text="开始练习"
android:id="@+id/button1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="@+id/textView1"
android:layout_toLeftOf="@+id/button2"
/>
<!-- 添加一个按钮button2,该按钮与textView1的右边界对齐 -->
<Button
android:text="不练习"
android:id="@+id/button2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignRight="@+id/textView1"
android:layout_below="@+id/textView1"
/>
</RelativeLayout>

通过这个例子,我们可以理解相对布局控件RelativeLayout的用法了。

Android应用开发学习之相对布局的更多相关文章

  1. Android应用开发学习之表格视图

    作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 本文我们来学习一个使用表格视图的程序,下图是该程序的运行效果: 该程序主Activity文件内容如下: packag ...

  2. 2021年正确的Android逆向开发学习之路

    2021年正确的Android逆向开发学习之路 说明 文章首发于HURUWO的博客小站,本平台做同步备份发布.如有浏览或访问异常或者相关疑问可前往原博客下评论浏览. 原文链接 2021年正确的Andr ...

  3. Android应用开发学习笔记之绘图

    作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 一.绘图常用类介绍 在Android中绘图时,常用到的几个类是Paint.Canvas.Bitmap和Bitmapt ...

  4. Android应用开发学习笔记之播放音频

    作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz Android支持常用音视频格式文件的播放,本文我们来学习怎样开发Android应用程序对音视频进行操作. Andr ...

  5. Android 系统开发学习杂记(转)

    http://blog.csdn.net/shagoo/article/details/6709430 > 开发环境1.安装 Eclipse 和 android-sdk 并解压安装2.Eclip ...

  6. Android应用开发学习笔记之AsyncTask

    作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 在上一篇文章中我们学习了多线程和Handler消息处理机制,如果有计算量比较大的任务,可以创建一个新线程执行计算工作 ...

  7. android移动开发学习笔记(二)神奇的Web API

    本次分两个大方向去讲解Web Api,1.如何实现Web Api?2.如何Android端如何调用Web Api?对于Web Api是什么?有什么优缺点?为什么用WebApi而不用Webservice ...

  8. Android应用开发学习笔记之Fragment

    作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz Fragment翻译成中文就是“碎片”.“片断”的意思,Fragment通常用来作为一个Activity用户界面的一 ...

  9. Android应用开发学习笔记之菜单

    作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz Android中的菜单分为选项菜单(OptionMenu)和上下文菜单(Context Menu).通常使用菜单资源 ...

随机推荐

  1. autofac meta

    http://kevincuzner.com/2014/05/19/extreme-attributed-metadata-autofac/ http://stackoverflow.com/ques ...

  2. 【笨嘴拙舌WINDOWS】API

    如今,相对于大行其道的对象,服务,API概念的提出要早很多,却依然经久不衰:所谓万变不离其宗,如今很多服务(Web Services,云服务)的提供方式和API如出一辙. Windows API(Ap ...

  3. python模拟http请求

    下文主要讲述如何利用python自带的库模拟http请求,为以后利用python做API测试做准备. 只讲述模拟http的过程,具体到自己用的时候,要以自己的应用为准做出适当的调整. #!coding ...

  4. HDU 4607 Park Visit (DP最长链)

    [题目]题意:N个城市形成一棵树,相邻城市之间的距离是1,问访问K个城市的最短路程是多少,共有M次询问(1 <= N, M <= 100000, 1 <= K <= N). [ ...

  5. ffmepg 指定RTSP网络连接模式UDP还是TCP

    AVFormatContext *formatCtx = NULL; formatCtx = avformat_alloc_context(); AVDictionary* options = NUL ...

  6. 强大的日志分析工具 -- NSLogger

    转:http://www.cnblogs.com/yingkong1987/p/3329945.html 强大的日志分析工具 -- NSLogger 源码:https://github.com/fpi ...

  7. 多线程监控文件夹,FlieSystemWatcher,并使用共享函数

    发表于: 2011-01-06 09:55:47   C# code   ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 2 ...

  8. 【转】从INF文件认识驱动

    在工控机安装xp操作系统时,由于工控机的集成显卡驱动只支持win7,之前没接触过windows驱动相关内容,折腾了半天.下载的驱动是exe的,双击安装就提示安装失败(未签名) 上图是网上随便找的,现象 ...

  9. 【c++内存分布系列】单继承

    父类包括成员函数.静态函数.静态方法,子类包括成员函数.静态函数.静态方法的情况与一个类时完全一致,这里就不做分析了.子类单独包含虚函数时继承无关,也不做分析了. 一.父类子类都为空 #include ...

  10. HDU 1671 Phone List(POJ 3630)

    Phone List Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...