相对布局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. hdu1869 六度分离(Floyd)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1869 转载请注明出处:http://blog.csdn.net/u012860063?viewmode ...

  2. Swift - 创建代理协议实现页面间参数传递和方法调用

    在开发中,经常需要用到协议代理模式.比如,进入编辑页面修改数据后,将新数据回传到主界面. 下面通过一个样例来说明协议代理模式,功能如下: 1,主页面有一个标签和一个修改按钮,点击修改按钮会跳转到编辑页 ...

  3. ALV判断修改后是否有不合法数据,有则选中错误行,高亮度显示。

    alv数据表维护表时错误行需要高亮度显示 gt_index_rows TYPE lvc_t_row,"用以存放要选择行的内表 gs_index_rows TYPE lvc_s_row.&qu ...

  4. 转]解析C语言中的sizeof

    解析C语言中的sizeof 一.sizeof的概念 sizeof是C语言的一种单目操作符,如C语言的其他操作符++.--等.它并不是函数.sizeof操作符以字节形式给出 了其操作数的存储大小.操作数 ...

  5. 用Delphi画圆角Panel的方法(使用CreateRoundRectRgn创造区域,SetWindowRgn显示指定区域)

    用Delphi画圆角Panel的方法: procedure TForm1.Button5Click(Sender: TObject);var fhr :Thandle;beginfhr:=Create ...

  6. jquery中怎么删除<ul>中的整个<li>包括节点

    .$('ul li').remove(); .$('ul li').each(function(){ $(this).remove(); }); .$("ul").find(&qu ...

  7. 如何配置Git支持大小写敏感和修改文件名中大小写字母呢?(转)

    1. 在新建代码文件时,不注意把文件名应该小小写搞错了2. 文件已经push到远程了3. 在windows下面将文件名字改为全小写 改好后,在Git中没有任何反应,使用git status时,如果遇到 ...

  8. Shell编程中Shift的用法(转)

    位置参数可以用shift命令左移.比如shift 3表示原来的$4现在变成$1,原来的$5现在变成$2等等,原来的$1.$2.$3丢弃,$0不移动.不带参数的shift命令相当于shift 1. 非常 ...

  9. phpc.sinaapp.com 加密的解密方法

    原文:phpc.sinaapp.com 加密的解密方法 很简单,用类似phpjm的解密方式,替换掉_inc.php中最后一个return中的eval为print就出来了.

  10. learning - Haskell AND Lisp vs. Haskell OR Lisp - Programmers Stack Exchange

    learning - Haskell AND Lisp vs. Haskell OR Lisp - Programmers Stack Exchange Haskell AND Lisp vs. Ha ...