相对布局 - Android

本篇文章记录的是android应用中的相对布局.

根据父容器定位属性

属性说明
layout_alignParentLeft左对齐
layout_alignParentRight右对齐
layout_alignParentTop顶部对齐
layout_alignParentBottom底部对齐
layout_centerHorizontal水平居中
layout_centerVertical垂直居中
layout_centerParent中间位置

根据兄弟组件定位的属性

属性说明
layout_toLeftOf设置参考组件,放置于参考组件的左边
layout_toRightOf设置参考组件,放置于参考组件的右边
layout_above设置参考组件,放置于参考组件的上方
layout_below设置参考组件,放置参考组件的下方
layout_alignTop设置参考组件,对齐参考组件的上边界
layout_alignBottom设置参考组件,对齐参考组件的下边界
layout_alignRight设置参考组件,对齐参考组件的右边界

代码示例

<?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"
    android:background="@color/pink"
    android:orientation="vertical">


    <ImageView
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:contentDescription="描述"
        android:scaleType="centerCrop"
        android:src="@drawable/images" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        android:gravity="center"
        android:orientation="vertical"

        >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="send"
            android:text="显示模态对话框" />


    </LinearLayout>
</RelativeLayout>

上面代码执行之后,会得到诸如下面的执行结果:

image-20230831052113394.png