RelativeLayout——兄弟组件定位

本文详细介绍了在Android开发中,使用RelativeLayout进行界面布局时的各种位置约束属性,包括如何将控件放置在参考组件的四周以及如何对其边界进行精确对齐。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

单个控件属性(2)
layout_toLeftOf(参考组件左方)
layout_toRightOf(参考组件右方)
layout_above(参考组件上方)
layout_below(参考组件下方)
layout_alignTop(对齐参考组件的上边界)
layout_alignBottom(对齐参考组件的下边界)
layout_alignLeft(对齐参考组件的左边界)
layout_alignRight(对齐参考组件的右边界)

  • 只有一个属性(1)
 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

    <Button
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="参考组件"
        android:layout_centerInParent="true"
        android:id="@+id/mainButton1"/>

    <Button
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="右方"
        android:layout_toRightOf="@id/mainButton1"/>

    <Button
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="下方"
        android:layout_below="@id/mainButton1"/>

    <Button
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="上方"
        android:layout_above="@id/mainButton1"/>

    <Button
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="左方"
        android:layout_toLeftOf="@id/mainButton1"/>

</RelativeLayout>

上效果
这里写图片描述

  • 只有一个属性(2)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

    <Button
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="参考组件"
        android:layout_centerInParent="true"
        android:id="@+id/mainButton1"/>

    <Button
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="右方"
        android:layout_alignRight="@id/mainButton1"/>

    <Button
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="下方"
        android:layout_alignBottom="@id/mainButton1"/>

    <Button
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="上方"
        android:layout_alignTop="@id/mainButton1"/>

    <Button
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="左边"
        android:layout_alignLeft="@id/mainButton1"/>

</RelativeLayout>

上图
这里写图片描述
图片中虽然看着只有两个,但是实际上却是左右两个重合在上方;上下两个重合在左方。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值