相對布局RelativeLayout(三)

image.png

目錄

什么是相對布局

相對布局是通過相對定位的方式讓控件出現(xiàn)在布局任意位置.

常見屬性

相對于父元素控件布局

屬性 含義
android:layout_centerHrizontal 水平居中
android:layout_centerVertical 垂直居中
android:layout_centerInparent 相對于父元素完全居中
android:layout_alignParentBottom 位于父元素的下邊緣
android:layout_alignParentLeft 位于父元素的左邊緣
android:layout_alignParentRight 位于父元素的右邊緣
android:layout_alignParentTop 位于父元素的上邊緣
android:layout_alignWithParentIfMissing 如果對應的兄弟元素找不到的話就以父元素做參照物

相對于某個元素控件布局

注意:屬性值必須為id的引用名“@id/id-name”

屬性 含義
android:layout_below 位于元素的下方
android:layout_above 位于元素的的上方
android:layout_toLeftOf 位于元素的左邊
android:layout_toRightOf 位于元素的右邊
android:layout_alignTop 該元素的上邊緣和某元素的的上邊緣對齊
android:layout_alignLeft 該元素的左邊緣和某元素的的左邊緣對齊
android:layout_alignBottom 該元素的下邊緣和某元素的的下邊緣對齊
android:layout_alignRight 該元素的右邊緣和某元素的的右邊緣對齊
相對像素值
屬性 含義
android:layout_marginBottom 底邊緣的距離
android:layout_marginLeft 左邊緣的距離
android:layout_marginRight 右邊緣的距離
android:layout_marginTop 上邊緣的距離

實戰(zhàn)

相對于父元素控件布局

使用相對水平和相對垂直實現(xiàn)控件居中:

android:layout_centerHorizontal="true" android:layout_centerVertical="true"

全部配置:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <RelativeLayout
        android:layout_width="368dp"
        android:layout_height="495dp"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="8dp">

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="相對布局" />
    </RelativeLayout>
</android.support.constraint.ConstraintLayout>

執(zhí)行程序:


image.png

相對于某個元素控件布局

使用和某元素的的左邊緣對齊 :

android:layout_alignLeft="@id/textView"

全部配置:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <RelativeLayout
        android:layout_width="368dp"
        android:layout_height="495dp"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="8dp">

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="相對布局" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@id/textView"

        android:text="test1" />

    </RelativeLayout>
</android.support.constraint.ConstraintLayout>

效果圖:


image.png

執(zhí)行程序:


image.png
相對像素值

使用左邊緣距離和上邊緣距離:

android:layout_marginLeft="20dp"
android:layout_marginTop="200dp"

全部配置:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <RelativeLayout
        android:layout_width="368dp"
        android:layout_height="495dp"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="8dp">

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="200dp"
            android:text="相對布局" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@id/textView"

        android:text="test1" />

    </RelativeLayout>
</android.support.constraint.ConstraintLayout>

效果圖:


image.png

執(zhí)行程序:


image.png

參考

最新Android開發(fā)視頻教程(共6章)Android Studio教程(2017-2018)
19 Android 相對布局的使用(視頻+筆記,從01開始點點入門)
Android studio 相對布局常見屬性
Android----------線性布局和相對布局的使用

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內容