相对布局RelativeLayout是Android布局中一个比較经常使用的控件,使用该控件能够布局出适合各种屏幕分辨率的布局,RelativeLayout採用相对位置进行控件属性设置.

能够设置控件与父控件的位置,控件与控件之间的位置。

1. 控件与父容器位置属性

android:layout_alignParentLeft="true"   子控件相对于父容器靠左边

                                 android:layout_alignParentTop="true"   
子控件相对于父容器靠 上边

                                 android:layout_marginTop="50dp"         
子控件与父容器上边距距离

                                android:layout_marginBottom="50dp"   
子控件与父容器下边距距离

                                android:layout_marginRight="50dp"     
子控件与父容器右边距距离

                                 android:layout_marginLeft="50dp"     
子控件与父容器左边距距离

android:layout_centerInParent="true"//子控件在父容器中居中显示

                                    android:layout_centerHorizontal="true" //子控件在父容器中水平居中

                                     android:layout_centerVertical="true"  子控件在父容器中垂直居中

以下的演示样例展示下相对于父容器的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="相对父easy布局"
android:background="#97FFFF" android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="50dp"
android:layout_marginBottom="50dp"
android:layout_marginRight="50dp" /> </RelativeLayout>

2.控件与控件间位置属性

控件与控件之间的位置属性,是指控件与相邻控件的位置设置,主要有下面属性

android:layout_below="@+id/textView1"    该控件位于指定控件的下方

                      android:layout_toLeftOf="@+id/textView1"    控件位于指定控件的左側

                      android:layout_toRightOf="@+id/textView1"    控件位于指定控件的右側

                     android:layout_above="@+id/textView1"           控件位于指定控件的上面

                     android:layout_alignBaseline="" 该控件的内容与指定控件的内容在同一直线上

android:layout_alignBottom=""该控件的底部与指定控件的底部对齐

android:layout_alignLeft="" 该控件与指定控件左側对齐

android:layout_alignRight="" 该控件与指定控件右側对齐

android:layout_alignTop="" 该控件与指定控件的顶部对齐

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" > <RelativeLayout
android:layout_width="match_parent"
android:layout_height="110dp"
android:id="@+id/layone"
> <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFD700"
android:text="标签1" /> <TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:background="#FF0000"
android:text="标签2" />
</RelativeLayout> <RelativeLayout
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_below="@+id/layone"
> <TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#EE9572"
android:text="标签3" /> <TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_toRightOf="@+id/textView3"
android:background="#CDAA7D" android:text="标签4" />
</RelativeLayout> </RelativeLayout>

3.商品列表演示样例

                       
以下我们展示一个商品列表,使用RelativeLayout来展示,效果图例如以下
                     
                          
                     代码
                     
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <RelativeLayout
android:layout_width="match_parent"
android:layout_height="120dp"
android:id="@+id/layout1"
> <ImageView
android:id="@+id/imageView1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:src="@raw/pad" /> <TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/imageView1"
android:layout_marginTop="10dp"
android:layout_toRightOf="@+id/imageView1"
android:text="商品名称:IPAD AIR" /> <TextView
android:id="@+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_toRightOf="@+id/imageView1"
android:layout_below="@+id/textView1"
android:text="商品价格:$19" /> <TextView
android:id="@+id/textView3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_toRightOf="@+id/imageView1"
android:layout_below="@+id/textView2"
android:text="商品颜色:白色" /> </RelativeLayout> <RelativeLayout
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="#CDAA7D"
android:layout_below="@+id/layout1"
android:id="@+id/layout2"
>
</RelativeLayout> <RelativeLayout
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_below="@+id/layout2"
android:id="@+id/layout3"
> <ImageView
android:id="@+id/imageView2"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:src="@raw/pad" /> <TextView
android:id="@+id/textView4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/imageView2"
android:layout_marginTop="10dp"
android:layout_toRightOf="@+id/imageView2"
android:text="商品名称:IPAD AIR" /> <TextView
android:id="@+id/textView5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_toRightOf="@+id/imageView2"
android:layout_below="@+id/textView4"
android:text="商品价格:$19" /> <TextView
android:id="@+id/textView6"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_toRightOf="@+id/imageView2"
android:layout_below="@+id/textView5"
android:text="商品颜色:白色" /> </RelativeLayout> <RelativeLayout
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="#CDAA7D"
android:layout_below="@+id/layout3"
>
</RelativeLayout> </RelativeLayout>






.Net程序猿玩转Android开发---(7)相对布局RelativeLayout的更多相关文章

  1. .Net程序猿玩转Android开发---(6)线性布局LinearLayout

                                LinearLayout控件是Android中重要的布局控件,是一个线性控件,所谓线性控件的意思是指该控件里面的内容仅仅能水平或垂直排列.也就 ...

  2. .Net程序猿玩转Android开发---(8)表格布局TableLayout

    表格布局TableLayout是Android中比較经常使用的一个布局控件,既然是表格,肯定有行和列,TableLayout中的行有TableRow组成.列依据每行控件的数量来确定 假如第一行有3个控 ...

  3. .Net程序猿玩转Android开发---(11)页面跳转

    在不论什么程序开发中,都会遇到页面之间跳转的情况,Android开发也不例外.这一节,我们来认识下Android项目中如何进行页面跳转.页面跳转分为有參数和无參数页面跳转,已经接受还有一个页面的返回值 ...

  4. .Net程序猿玩转Android开发---(3)登陆页面布局

    这一节我们来看看登陆页面如何布局.对于刚接触到Android开发的童鞋来说.Android的布局感觉比較棘手.须要结合各种属性进行设置,接下来我们由点入面来 了解安卓中页面如何布局,登陆页面非常eas ...

  5. .Net程序员玩转Android开发--ListView单击事件

    public class ListViewClickActivity extends Activity {         private ListView lv;        SimpleAdap ...

  6. .Net程序员玩转Android系列之一~Java快速入门

    前言 前段时间受公司业务发展需要,探索性进入Android开发领域.一切从零开始,java基础,Java进阶,Android框架学习,Eclipse熟悉,最终到第一个即时通讯App完成,历经一个月的时 ...

  7. 作为一个程序员怎么通过android开发赚钱

    ​ 上面是一个程序员通过Android开发每天的收入,信则有! 自己学安卓差不多,有一年了.我本来是从事javaweb开发的,可能学习安卓上手会快点.其实安卓没有那难 .首先开发安卓程序,要有一个,开 ...

  8. Android开发-之五大布局

    在html中大家都知道布局是什么意思了,简单来说就是将页面划分模块,比如html中的div.table等.那么Android中也是这样的.Android五大布局让界面更加美化,开发起来也更加方便.当然 ...

  9. Android开发5大布局方式详解

    Android中常用的5大布局方式有以下几种: 线性布局(LinearLayout):按照垂直或者水平方向布局的组件. 帧布局(FrameLayout):组件从屏幕左上方布局组件. 表格布局(Tabl ...

随机推荐

  1. Linux内核参数信息(Oracle相关)

    命令行:vim  /etc/sysctl.conf 查看如下两行的设置值,这里是: kernel.shmall = 2097152 kernel.shmmax = 4294967295 如果系统默认的 ...

  2. Oracle ACL(Access Control List)

    在oralce 11g中假如你想获取server的ip或者hostname,执行如下语句 SELECT utl_inaddr.get_host_address FROM dual;  //获取IP S ...

  3. [Android学习笔记]Context简单理解

    一.Context是什么?上下文对象,可以理解为一个程序的运行的环境,从中可以获取当前程序的资源:getResources,getAssets 二.常见的Context有哪些?Application ...

  4. 更改windows系统语言,改为英文

  5. SilkTest Q&A 2

    Q11:SilkTest中有没有计算web页面上单词数量的函数? A11:你可以使用Clipboard函数.使用Ctrl+a和Ctrl+c,然后解析string的list. Q12:silktest的 ...

  6. hdu2066一个人的旅行

    枚举全部相邻城市,作为起点,多次spfa,然后每次在想去的城市中找出spfa后的距离起点最短的花费时间 #include <iostream> #include <cstring&g ...

  7. 如何理解java回电话

    同android中间Button的setOnClickListener这个回调案例: Button button = (Button)this.findViewById(R.id.button); b ...

  8. 【css基础】垂直外边距的合并

    近期在重温<CSS权威指南>,还是想把基础再打坚固点,如今对垂直外边距的合并问题进行简单总结. 1. 两个块级元素的外边距都大于0时,取那个最大值作为两个块级元素的垂直边距 请看以下一个小 ...

  9. Storm集群中执行的各种组件及其并行

    一.Storm中执行的组件      我们知道,Storm的强大之处就是能够非常easy地在集群中横向拓展它的计算能力,它会把整个运算过程切割成多个独立的tasks在集群中进行并行计算.在Storm中 ...

  10. jquery.ui.accordion的修改(支持展开多个)

    原文:jquery.ui.accordion的修改(支持展开多个) 背景:原jquery.ui.accordion插件,最多只能展开一个,不能展开多个,后来在网上找到了一个基于它的一个修改版(http ...