Android基础学习笔记10:相对布局

学习目标

能说出相对布局常用的属性
能利用相对布局实现比较复杂的界面设计

一、相对布局概述

1、布局特点

在相对布局中,一个控件的位置取决于它和其它控件的相对关系。

优点:使用比较灵活。
缺点:复杂,掌握较难。

2、继承关系图

在这里插入图片描述

3、常用属性

(1)相对于父容器居中

layout_centerInParent 在父容器居中
layout_centerHorizontal 在父容器水平居中
layout_centerVertical 在父容器垂直居中

(2)相对于其它控件的位置

layout_toLeftOf 在……左边
layout_toRightOf 在……右边
layout_above 在……上面
layout_below 在……下面

(3)相对于其它控件对齐

layout_alignLeft 与……左对齐
layout_alignRight 与……右对齐
layout_alignTop 与……顶对齐
layout_alignBottom 与……底对齐
layout_alignBaseLine 与……基线对齐

(4)相对于父容器对齐

layout_alignParentLeft 与父容器左对齐
layout_alignParentRight 与父容器右对齐
layout_alignParentTop 与父容器顶对齐
layout_alignParentBottom 与父容器底对齐

(5)标识符问题

@+id/button —— 创建新的id(建议使用)
@id/button —— 引用已有的id

二、案例演示——相对布局演示

(一)运行效果

在这里插入图片描述

(二)实现步骤

1、创建安卓应用【RelativeLayoutDemo】

2、主布局资源文件activity_main.xml

分析界面:首先确定【中央】按钮,然后其它按钮可以根据与它的相对位置关系来定位。【左上角】按钮、【右上角】按钮、【左下角】按钮与【右下角】按钮可以根据它与父容器的对齐方式来确定。

<?xml version="1.0" encoding="utf-8"?>

<Button
    android:id="@+id/btnCenter"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="中央"/>

<Button
    android:id="@+id/btnUpperLeft"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@id/btnCenter"
    android:layout_above="@id/btnCenter"
    android:text="左上"/>

<Button
    android:id="@+id/btnUpperRight"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/btnCenter"
    android:layout_above="@id/btnCenter"
    android:text="右上"/>

<Button
    android:id="@+id/btnLowerLeft"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@id/btnCenter"
    android:layout_below="@id/btnCenter"
    android:text="左下"/>

<Button
    android:id="@+id/btnLowerRight"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/btnCenter"
    android:layout_below="@id/btnCenter"
    android:text="右下"/>

<Button
    android:id="@+id/btnOK"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/btnLowerLeft"
    android:layout_alignLeft="@id/btnLowerLeft"
    android:layout_marginTop="15dp"
    android:text="确定"/>

<Button
    android:id="@+id/btnCancel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/btnLowerRight"
    android:layout_alignLeft="@id/btnLowerRight"
    android:layout_marginTop="15dp"
    android:text="取消"/>

<Button
    android:id="@+id/btnUpperLeftCorner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="左上角"/>

<Button
    android:id="@+id/btnUpperRightCorner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="右上角"/>

<Button
    android:id="@+id/btnLowerLeftCorner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentBottom="true"
    android:text="左下角"/>

<Button
    android:id="@+id/btnLowerRightCorner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true"
    android:text="右下角"/>

查看设计视图
在这里插入图片描述

3、启动应用,查看效果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

土豆_wk

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值