(一) RelativeLayout--相对布局,指按照控件之间的相对位置来进行布局

(二) RelativeLayout的特有属性:

1、android:gravity 设置布局容器内子控件的对齐方式 2、android:ignoreGravity 设置布局管理器内哪个控件不受gravity属性的影响

(三) RelativeLayout子控件的可用属性:

根据RelativeLayout内部子控件的排列方式,可以将属性分为3组:

A、第一组:指 控件与父布局之间的对齐关系。该组属性的值是true或者false。(摆放的参照点是父容器)
    1. layout_alignParentRight 该控件与父布局控件右对齐
    2. layout_alignParentLeft 该控件与父布局控件左对齐
    3. layout_alignParentTop 该控件与父布局控件顶端对齐
    4. layout_alignParentBottom 该控件与父布局控件底部对齐
    5. layout_centerInParent 该控件位于父布局控件中心位置
    6. layout_centerVertical 该控件位于父布局控件垂直中心位置
    7. layout_centerHorizontal 该控件位于父布局控件水平中心位置
B、第二组:指兄弟控件之间的相对位置。该组属性的值是另一个控件的id。(摆放的参照点是其他的同级控件)
    1. layout_toRightOf 该控件在哪个控件的右侧
    2. layout_toLeftOf 该控件在哪个控件的左侧
    3. layout_above 该控件在哪个控件的上侧
    4. layout_below 该控件在哪个控件的下侧
C、第三组:指 兄弟控件之间 的对齐关系。该组属性的值是另一个控件的id。
    1. layout_alignRight 该控件与哪个控件的右对齐
    2. layout_alignLeft 该控件与哪个控件的左对齐
    3. layout_alignTop 该控件与哪个控件的顶对齐
    4. layout_alignBottom 该控件与哪个控件的底对齐

下面使用三个案例依次说面这三组的使用反式:
以下代码使用的是第一组的属性,相对布局内部只有一个TextView显示在屏幕中心位置
<?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" >        <!--     第一组:    控件与父布局之间的对齐关系。该组属性的值是true或者false    layout_alignParentRight          该控件与父布局控件右对齐layout_alignParentLeft           该控件与父布局控件左对齐layout_alignParentTop            该控件与父布局控件顶端对齐layout_alignParentBottom         该控件与父布局控件底部对齐layout_centerInParent            该控件位于父布局控件中心位置layout_centerVertical            该控件位于父布局控件垂直中心位置layout_centerHorizontal          该控件位于父布局控件水平中心位置     -->    <TextView        android:id="@+id/textCenter"        android:text="中心"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true" /></RelativeLayout>

运行效果如下:
如果想在中心上方显示一个"上"文本信息,则需要使用第二组属性,具体使用如下图中的第二个TextView:
<?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" >        <!--     第一组:    控件与父布局之间的对齐关系。该组属性的值是true或者false    layout_alignParentRight          该控件与父布局控件右对齐layout_alignParentLeft           该控件与父布局控件左对齐layout_alignParentTop            该控件与父布局控件顶端对齐layout_alignParentBottom         该控件与父布局控件底部对齐layout_centerInParent            该控件位于父布局控件中心位置layout_centerVertical            该控件位于父布局控件垂直中心位置layout_centerHorizontal          该控件位于父布局控件水平中心位置     -->    <TextView        android:id="@+id/textCenter"        android:text="中心"        android:textSize="30dp"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true" />        <!--     第二组:    兄弟控件之间的相对位置。该组属性的值是另一个控件的id。(摆放的参照点是其他的同级控件)layout_toRightOf      该控件在哪个控件的右侧layout_toLeftOf       该控件在哪个控件的左侧layout_above          该控件在哪个控件的上侧layout_below          该控件在哪个控件的下侧     -->     <TextView         android:id="@+id/textAbove"         android:text="上"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_above="@id/textCenter" /></RelativeLayout>

运行效果如下:
从图上可以看出,虽然"上"已经显示在中心的上方,但是并没有跟中心左对齐,如果需要将控件显示在中心的正上方或者正下方,则需要使用第三组属性,具体使用如下图中的第三个TextView:
<?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" >        <!--     第一组:    控件与父布局之间的对齐关系。该组属性的值是true或者false    layout_alignParentRight          该控件与父布局控件右对齐layout_alignParentLeft           该控件与父布局控件左对齐layout_alignParentTop            该控件与父布局控件顶端对齐layout_alignParentBottom         该控件与父布局控件底部对齐layout_centerInParent            该控件位于父布局控件中心位置layout_centerVertical            该控件位于父布局控件垂直中心位置layout_centerHorizontal          该控件位于父布局控件水平中心位置     -->    <TextView        android:id="@+id/textCenter"        android:text="中心"        android:textSize="30sp"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true" />        <!--     第二组:    兄弟控件之间的相对位置。该组属性的值是另一个控件的id。(摆放的参照点是其他的同级控件)layout_toRightOf      该控件在哪个控件的右侧layout_toLeftOf       该控件在哪个控件的左侧layout_above          该控件在哪个控件的上侧layout_below          该控件在哪个控件的下侧     -->     <TextView         android:id="@+id/textAbove"         android:text="上"         android:textSize="30sp"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_above="@id/textCenter" />          <!--      第三组:     兄弟控件之间的对齐关系。该组属性的值是另一个控件的id。layout_alignRight       该控件与哪个控件的右对齐layout_alignLeft        该控件与哪个控件的左对齐layout_alignTop         该控件与哪个控件的顶对齐layout_alignBottom      该控件与哪个控件的底对齐      -->      <TextView         android:id="@+id/textBelow"         android:text="正下"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_below="@id/textCenter"         android:layout_alignLeft="@id/textCenter" />      </RelativeLayout>

运行效果如下图:

综上所述:RelativeLayout的属性一般是3组属性结合使用,来达到需要的显示效果

更多相关文章

  1. android studio - swiperefreshlayout注意点
  2. 仿Android印象笔记底部导航栏
  3. 安卓如何创建右上角点击菜单
  4. android常用控件一二
  5. ConfigurationTest以及横竖屏切换及2.3与4.0Configuration的区别
  6. Android控件 TimePicker
  7. Android(安卓)ListView(加载不同布局)嵌套GridView(加载不同布局)
  8. 关于ScrollView嵌套RecyclerView时RecyclerView不显示的问题
  9. Android的页面布局小结

随机推荐

  1. Android实训案例(九)——答题系统的思绪,自
  2. Android如何正确的保存文件
  3. Android高效率编码-第三方SDK详解系列(三
  4. 在android中使用Get方式提交数据
  5. Android仿iOS实现三级联动选择器
  6. android EditText多行文本输入的若干问题
  7. 如何在Android的模拟器中的SD卡中添加文
  8. 基于WebRTC的Android数字楼宇对讲系统回
  9. Android事件管理源码剖析
  10. Android(安卓)TextView自动换行文字排版