Android布局之RelativeLayout

RelativeLayout相对布局是让子控件相对于兄弟控件或父控件进行布局,可以设置子空间相对于兄弟或父控件进行上下左右对齐。
RelativeLayout常用属性:
        1、相对于父控件,例如:android:layout_alignParentTop=“true”
        android:layout_alignParentTop      控件的顶部与父控件的顶部对齐;
        android:layout_alignParentBottom  控件的底部与父控件的底部对齐;
        android:layout_alignParentLeft      控件的左部与父控件的左部对齐;
        android:layout_alignParentRight     控件的右部与父控件的右部对齐;

        2、相对给定Id控件,例如:android:layout_above=“@id/**”
        android:layout_above 控件的底部置于给定ID的控件之上;
        android:layout_below     控件的底部置于给定ID的控件之下;
        android:layout_toLeftOf    控件的右边缘与给定ID的控件左边缘对齐;
        android:layout_toRightOf  控件的左边缘与给定ID的控件右边缘对齐;
        android:layout_alignBaseline  控件的baseline与给定ID的baseline对齐;
        android:layout_alignTop        控件的顶部边缘与给定ID的顶部边缘对齐;
        android:layout_alignBottom   控件的底部边缘与给定ID的底部边缘对齐;
        android:layout_alignLeft       控件的左边缘与给定ID的左边缘对齐;
        android:layout_alignRight      控件的右边缘与给定ID的右边缘对齐;

        3、居中,例如:android:layout_centerInParent=“true”
        android:layout_centerHorizontal 水平居中;
        android:layout_centerVertical    垂直居中;
        android:layout_centerInParent  父控件的中央;

效果如:这里写图片描述

相对布局就是一定要加Id才能管理。

<?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/title"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:text="相对布局的试用"
    android:gravity="center"
    android:background="#1000"/>
<TextView
    android:id="@+id/title2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="二级标题"
    android:layout_below="@id/title"
    android:layout_alignParentRight="true"/>


    <TextView
        android:id="@+id/center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="中间"
        android:textSize="40sp"
        android:layout_margin="30dp"
        android:layout_centerInParent="true"/>

    <TextView
        android:id="@+id/bottom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="下面"
        android:layout_below="@id/center"
        android:layout_centerInParent="true"
        android:textSize="20sp"/>

    <TextView
        android:id="@+id/left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="左面"
        android:textSize="20sp"
        android:layout_centerInParent="true"
        android:layout_toLeftOf="@id/center"/>

    <TextView
        android:id="@+id/right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="右面"
        android:textSize="20sp"
        android:layout_centerInParent="true"
        android:layout_toRightOf="@id/center"/>

    <TextView
        android:id="@+id/top"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="上面"
        android:textSize="20sp"
        android:layout_centerInParent="true"
        android:layout_above="@id/center"/>
</RelativeLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值