相对布局要比前面讲的线性布局和表格布局要灵活一些,所以平常用得也是比较多的。相对布局控件的位置是与其周围控件的位置相关的,从名字可以看出来,这些位置都是相对的,确定出了其中一个控件的位置就可以确定另一个控件的位置了。

本次实验就是显示如下的activity:

    

其中只有2个button,1个textview,1个edittext。

在相对布局中,一般用到的控件属性解释如下:

在相对布局中有如下属性,解释如下:

android:layout_above  为将该控件的底部放在指定id控件的上方

android:layout_below   同理类似,将该控件的顶部放在指定id控件的下方

android:layout_toLeftOf  将该控件的右端紧挨着放在指定id控件的左端。

android:layout_toRightOf  将该控件的左端紧挨着放在指定id控件的右端

android:layout_alignParentRight  为true时将该控件右端与父控件右端对齐

android:layout_alignParentLeft       为true时将该控件左端与父控件左端对齐

android:layout_alignParentTop       为true时将该控件顶端与父控件顶端对齐

android:layout_alignParentBottom  为true时将该控件底端与父控件底端对齐

android:layout_alignBottom            将该控件底部与指定id控件底部控件对齐

android:layout_alignLeft                   将该控件左边与指定id控件左边对齐

android:layout_alignRight                 将该控件右边与指定id控件右边对齐

android:layout_alignTop                    将该控件顶端与指定id控件顶端对齐

实现上面activity比较简单,其xml代码如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10px" > <TextView
android:id="@+id/input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/input_dis"
tools:context=".MainActivity" /> <EditText
android:id="@+id/edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/input"
android:background="@android:drawable/editbox_background"
/> <Button
android:id="@+id/ok"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="@id/edit"
android:layout_alignParentRight="true"
android:layout_marginLeft="10px"
android:text="@string/ok"
/> <Button
android:id="@+id/cancel"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="@id/edit"
android:layout_toLeftOf="@id/ok"
android:text="@string/cancel" /> </RelativeLayout>

总结:activity的相对布局比较灵活,一些常见的属性也比较多,用得多自然就会了。

Android基础_3 Activity相对布局的更多相关文章

  1. Android基础_2 Activity线性布局和表格布局

    在activity的布局中,线性布局和表格布局是最简单的,这次分别从线性布局,表格布局以及线性布局和表格混合布局做了实验,实验中只需要编写 相应的xml的代码,java代码不需要更改,因为我们这里只是 ...

  2. android 基础02 - Activity 的生命周期及状态

    返回栈 Android 中的 Activity 是可以层叠的,当我们启动一个新的 Activity 时,就会覆盖在原有的 Activity 之上, 点击 Back 会销毁当前 Activity,下面的 ...

  3. Android基础之Activity launchMode详解

    本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! Activity的lauchmode,是基础的属性,但也是App优化必须掌握的知识,它约束了Acti ...

  4. Android基础之Activity

    一.什么是Activity Activity是Android四大组件之一,并且Activity是组件中的重中之重. Activity是为用户提供一个用于信息交互的窗口. 二.如何去创建Activity ...

  5. <Android基础>(二) Activity Part 2

    1.活动生命周期 1)返回栈 2)活动状态 3)活动的生存期 2.活动的启动模式 1)standard 2)singleTop 3)singleTask 4)singleInstance 3.活动的优 ...

  6. <Android基础>(二) Activity Part 1

    1.活动的基本用法: 1) 手动创建活动.创建加载布局 2) 在AndroidManifest文件中注册 3) 在活动中添加Button.Toast.Menu 4) 销毁活动 2.Intent 1) ...

  7. Android基础之Activity四种启动模式

    这东西是最基础的,发现自己其实没有真正试过,好好研究研究 standard :默认, singleTop :大体上同standard,但是当该Activity实例已经在task栈顶,不再创建新的实例, ...

  8. 【Android基础】Activity之间进行参数传递的三种方式

    1.使用Intent进行传输 //发送数据的Activity class button implements OnClickListener{ @Override public void onClic ...

  9. android基础知识复习——RelativeLayout布局属性、背景、半透明设置(XML设置)

    转自:http://blog.csdn.net/fansongy/article/details/6817968 复习布局与XML,写了一个空的登录界面.XML的注释我写在当行的后面了.程序运行图: ...

随机推荐

  1. C# book

    <编写高质量代码:改善C#程序的157个建议>源码下载 http://www.cnblogs.com/luminji/archive/2011/09/20/2182265.html < ...

  2. silverlight 控件自定义样式 实现方法

    1:在app.xaml中加入需实现的样式,如: <Application.Resources> <Style x:Key="NodeStyle" TargetTy ...

  3. 3573: [Hnoi2014]米特运输 - BZOJ

    Description米特是D星球上一种非常神秘的物质,蕴含着巨大的能量.在以米特为主要能源的D星上,这种米特能源的运输和储存一直是一个大问题.    D星上有N个城市,我们将其顺序编号为1到N,1号 ...

  4. 【POJ】【2420】A Star not a Tree?

    模拟退火 Orz HZWER 这题的题意是在二维平面内找一点,使得这点到给定的n个点的距离和最小……0.0 模拟退火算法请戳这里 //POJ 2420 #include<ctime> #i ...

  5. Sql注入中连接字符串常用函数

    在select数据时,我们往往需要将数据进行连接后进行回显.很多的时候想将多个数据或者多行数据进行输出的时候,需要使用字符串连接函数.在sqli中,常见的字符串连接函数有concat(),group_ ...

  6. python的dict()函数

    dict(one=1,two=2) dict({'one':1,'two':2}) dict((('one',1),('two',2))) dict((['one',1],['two',2])) di ...

  7. Oracle/PLSQL: Creating Functions

    In Oracle, you can create your own functions. 译:在ORACLE中,你可以创建你自己的方法. The syntax for a function is: ...

  8. secure CRT 介绍

        SecureCRT是一款支持SSH(SSH1和SSH2)的终端仿真程序,简单地说是Windows下登录UNIX或Linux服务器主机的软件.     Secure[1] CRT支持SSH,同时 ...

  9. 00 - Oracle体系结构课程内容

    1.导论 自己把本书内容分为几个部分, (1)体系结构,(2)并发控制和事务,(3)物理存储, (4)其他. 嗯,拿到一个新的数据库,我们也要从这几个方面去认识它.   2.体系结构 3.Oracle ...

  10. 阻止事件冒泡(stopPropagation和cancelBubble)和阻止默认行为(preventDefault和returnValue)

    <div id="divId1" style="width:500px;height:500px;background-color:#3ac;text-align: ...