RelativeLayout布局实验(Android Studio)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/rothschild666/article/details/102545646

RelativeLayout布局实验(Android Studio)

在这里插入图片描述
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/parent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="false"
    android:layout_alignParentTop="false"
    android:layout_alignParentRight="false"
    android:layout_alignParentBottom="false"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/b1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="parentstart" />

    <Button
        android:id="@+id/b2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="166dp"
        android:layout_toRightOf="@id/b1"
        android:text="parentright" />

    <Button
        android:id="@+id/b3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/b1"
        android:layout_marginTop="622dp"
        android:text="parentbottom" />

    <Button
        android:id="@+id/b4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="160dp"
        android:layout_marginTop="670dp"
        android:layout_toRightOf="@id/b3"
        android:text="parentleft" />

    <Button
        android:id="@+id/cen"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@color/colorAccent"
        android:text="center" />

    <Button
        android:id="@+id/above"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/cen"
        android:layout_marginBottom="20dp"
        android:layout_marginLeft="160dp"
        android:text="above" />

    <Button
        android:id="@+id/left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/cen"
        android:layout_marginBottom="0dp"
        android:layout_marginLeft="50dp"
        android:text="left" />

    <Button
        android:id="@+id/right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/cen"
        android:layout_marginLeft="270dp"
        android:layout_marginBottom="0dp"
        android:text="right" />

    <Button
        android:id="@+id/below"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/cen"
        android:layout_marginLeft="160dp"
        android:layout_marginTop="20dp"
        android:text="below" />

</RelativeLayout>

RelativeLayout(相对布局)中,控件可以通过两类属性表示相对位置,一类表示与父容器的相对位置,另一类表示与其他控件之间的相对位置。不管是相对于父容器位置还是其他控件位置(容器),在设置好大致方向之后,一般都是需要再设置具体方向的距离而且一般是两个(上下一个、左右一个)。

一、大致方位:相对于父容器的属性,属性值选择 true 或者 false。

android:layout_centerInParent(在父容器的水平、垂直方向都居中)
android:layout_centerVertical(在父类的垂直方向居中)
android:layout_centerHorizontal(在父类的水平方向居中)
android:layout_alignParentTop(在父容器最上)
android:layout_alignParentBottom(在父容器最下)
android:layout_alignParentLeft(在父容器最左)
android:layout_alignParentRight(在父容器最右)

二、大致方位:相对于其他控件的属性,属性值为控件的id。

android:layout_above(位于某控件上方)
android:layout_below(位于某控件下方)
android:layout_toLeftOf(位于某控件左方)
android:layout_toRightOf(位于某控件右方)
android:layout_alignTop(与某控件顶部对齐)
android:layout_alignBottom(与某控件底部对齐)
android:layout_alignLeft(与某控件左边缘对齐)
android:layout_alignRight(与某控件右边缘对齐)
android:layout_alignBaseline(与某控件的文本内容在一条直线上)

三、具体方向的距离:属性值单位选择为dp。

android:layout_margin(和父容器或者其他控件四周的距离)
android:layout_marginTop(和父容器或者其他控件上端的距离)
android:layout_marginBottom(和父容器或者其他控件下端的距离)
android:layout_marginLeft(和父容器或者其他控件左端的距离)
android:layout_marginRight(和父容器或者其他控件右端的距离)

猜你喜欢

转载自blog.csdn.net/rothschild666/article/details/102545646