Android开发Android技术知识Android进阶之路

Android应用开发笔记之相对布局RelativeLayout

2019-07-04  本文已影响1人  Lee_5566
image.png

目录

第一篇:Android应用开发笔记之Android Studio第一个程序(一)
第二篇:Android应用开发笔记之线性布局LinearLayout(二)
第三篇:Android应用开发笔记之线性布局LinearLayout(二)小练习
第四篇:Android应用开发笔记之相对布局RelativeLayout(三)

什么是相对布局

相对布局是通过相对定位的方式让控件出现在布局任意位置.

常见属性

相对于父元素控件布局

属性 含义
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 上边缘的距离

实战

相对于父元素控件布局

使用相对水平和相对垂直实现控件居中:

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>

执行程序:


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

执行程序:


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

执行程序:


image.png

参考

最新Android开发视频教程(共6章)Android Studio教程(2017-2018)
19 Android 相对布局的使用(视频+笔记,从01开始点点入门)
Android studio 相对布局常见属性
Android----------线性布局和相对布局的使用

上一篇下一篇

猜你喜欢

热点阅读