android layouts之RelativeLayout



相对布局管理器中控件的位置是按照相对其他控件的位置来呈现的。其属性有如下几类。

  (一)、第一类:属性值为true或false
  android:layout_centerHrizontal 水平居中
  android:layout_centerVertical 垂直居中
  android:layout_centerInparent 相对于父元素完全居中
  android:layout_alignParentBottom 贴紧父元素的下边缘
  android:layout_alignParentLeft 贴紧父元素的左边缘
  android:layout_alignParentRight 贴紧父元素的右边缘
  android:layout_alignParentTop 贴紧父元素的上边缘  

  (二)、第二类:属性值必须为id的引用名“@id/id-name
  android:layout_below 在某元素的下方
  android:layout_above 在某元素的的上方
  android:layout_toLeftOf 在某元素的左边
  android:layout_toRightOf 在某元素的右边
  android:layout_alignTop 本元素的上边缘和某元素的的上边缘对齐
  android:layout_alignLeft 本元素的左边缘和某元素的的左边缘对齐
  android:layout_alignBottom 本元素的下边缘和某元素的的下边缘对齐
  android:layout_alignRight 本元素的右边缘和某元素的的右边缘对齐


  (三)、第三类:属性值为具体的像素值,如30dip,40px  也就是外边框
  android:layout_marginBottom 离某元素底边缘的距离
  android:layout_marginLeft 离某元素左边缘的距离
  android:layout_marginRight 离某元素右边缘的距离
  android:layout_marginTop 离某元素上边缘的距离




下面用代码来呈现具体属性值的效果

xml代码如下



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"   定义相对布局
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_orange_dark">
    
    <TextView
        android:id="@+id/TextView1"
        android:text="@string/txt1"
        android:textSize="30sp"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_red_dark"
        android:textColor="@android:color/white"/>
    
    <TextView
        android:id="@+id/TextView2"
        android:text="@string/txt2"
        android:layout_below="@id/TextView1"   放在TextView1的下面
        android:textSize="30sp"
        android:gravity="center"      将字体放置在控件的中央
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_blue_dark"
        android:textColor="@android:color/white"/>
    
    <TextView
        android:id="@+id/TextView3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/TextView4"
        android:text="@string/txt3"
        android:layout_alignParentBottom="true"    放在父空间的底部
        android:textSize="30sp"
        android:gravity="center"
        android:background="@android:color/holo_blue_dark"
        android:textColor="@android:color/white"/>
    
    <TextView
        android:id="@+id/TextView4"
        android:text="@string/txt4"
        android:layout_centerInParent="true"<span style="white-space:pre">	</span>  并放在中央
        android:textSize="30sp"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_purple"
        android:textColor="@android:color/white"/>
    
    <TextView
        android:id="@+id/TextView5"
        android:text="@string/txt5"
        android:layout_alignParentBottom="true"    放在父空间的底部
         android:layout_toRightOf="@id/TextView4"  并放在TextView4的右边
        android:textSize="30sp"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/darker_gray"
        android:textColor="@android:color/white"/>
    
    

</RelativeLayout>

最后呈现的效果就是这样的。




该布局管理器使用灵活方便。可以设置不同的实现及相对位置来实现控件的组织。


下面用代码来呈现具体属性值的效果

xml代码如下


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值