icode icode
首页
  • Android学习

    • 📁基础内容
    • 📺AndroidCore
    • 🎨Android-UI
    • 🏖️Components
    • 📊Fragment
    • 🔗网络操作
    • 🔏异步机制
    • 📦数据存储
    • 🗃️Gradle
  • 学习笔记

    • 『框架』笔记
    • 『Kotlin』笔记
    • 《Vue》笔记
    • 《Git》学习笔记
    • 『Bug踩坑记录』
  • ListView
  • RecyclerView
  • ViewPager
  • Java笔记

    • 🟠JavaSE
    • 🟢JavaWeb
    • 🔴JavaEE
    • ⚪JavaTopic
    • 🍳设计模式
  • 计算机基础

    • 📌计算机网络
    • 🔍数据结构
    • 📦数据库
    • 💻OS
  • 技术文档
  • GitHub技巧
  • Nodejs
  • 博客搭建
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
  • 关于

    • 📫关于我
  • 收藏

    • 网站
    • 资源
    • Vue资源
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

iqqcode

保持对技术的探索实践与热爱
首页
  • Android学习

    • 📁基础内容
    • 📺AndroidCore
    • 🎨Android-UI
    • 🏖️Components
    • 📊Fragment
    • 🔗网络操作
    • 🔏异步机制
    • 📦数据存储
    • 🗃️Gradle
  • 学习笔记

    • 『框架』笔记
    • 『Kotlin』笔记
    • 《Vue》笔记
    • 《Git》学习笔记
    • 『Bug踩坑记录』
  • ListView
  • RecyclerView
  • ViewPager
  • Java笔记

    • 🟠JavaSE
    • 🟢JavaWeb
    • 🔴JavaEE
    • ⚪JavaTopic
    • 🍳设计模式
  • 计算机基础

    • 📌计算机网络
    • 🔍数据结构
    • 📦数据库
    • 💻OS
  • 技术文档
  • GitHub技巧
  • Nodejs
  • 博客搭建
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
  • 关于

    • 📫关于我
  • 收藏

    • 网站
    • 资源
    • Vue资源
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 基础内容

  • AndroidCore

  • Android UI

    • 布局

      • 通用属性
      • 常用布局
      • 线性布局LinearLayout
      • 相对布局RelativeLayout
        • 相对布局的重要属性
          • 参照父容器
          • !image-20201124150209292控件
        • 基础属性
          • 1.位置属性
          • 2.尺寸属性
          • 3.id引用属性
        • RelativeLayout的坑
        • 页面Demo
      • ConstraintLaout使用
      • ConstraintLayout辅助类
      • 动态设置字体样式
      • 表格布局TableLayout
      • 抽屉布局DrawerLayout
      • NavigationView侧边栏
      • CoordinatorLayout
      • 让一个控件按钮居于底部
      • 样式和主题
      • ConstraintLayout使用详解
      • 什么时候该用约束布局ConstraintLayout
      • ConstraintLayout官方文档
      • ConstraintLayout高级内容
    • 基础控件

    • 布局优化

    • View绘制

  • Components

  • Fragment

  • 网络操作

  • 异步机制

  • 数据存储

  • 学习笔记

  • 自定义View

  • View事件体系

  • Android
  • Android UI
  • 布局
iqqcode
2022-06-05
目录

相对布局RelativeLayout

# 相对布局的重要属性

  • [x] 关键是选取参照物,是父容器还是其他控件
  • [x] 布局位置与代码的前后顺序无关,只与属性设置有关

相对于父容器(取值:true/false)

android:layout_alignParentRight
1

相对于其他控件(取值:其他控件id)

android:layout_toRightOf
1

# 参照父容器

【参照格式】

android:layout_centerInParent="true"
1

属性值

1 - android:layout_centerInParent    正中位置
2 - android:layout_alignParentLeft   左上
3 - android:layout_alignParentRight  右上
4 - android:layout_alignParentTop    顶部(偏左)
5 - android:layout_alignParentBottom 底部(偏左)
6 - android:layout_centerVertical    中部(偏左)
7 - android:layout_centerHorizontal  顶部中间
1
2
3
4
5
6
7

# image-20201124150209292控件

参照某一id控件的相对位置

android:layout_above="@id/center"
1

I. 【在参照物的某边】

  • android:layout_above: 上边
  • android:layout_below: 下边
  • android:layout_toLeftOf: 左边
  • android:layout_toRightOf: 右边

image-20201124151834758

II. 和参照物的某边线对齐

相对于控件定位的效果

RelativeLayout中还有另外一组相对于控件进行定位的属性,android:layout_alignLeft 表示让一个控件的左边缘和另一个控件的左边缘对齐,android:layout_alignRight 表示让一个控件的右边缘和另一个控件的右边缘对齐。此外,还有android:layout_alignTop 和android:layout_alignBottom ,道理都是一样的,我就不再多说,这几个属性就留给你自己去尝试吧。

  • android:layout_alignTop: 上边线
  • android:layout_alignBottom: 下边线
  • android:layout_alignLeft: 左边线
  • android:layout_alignRight: 右边线
<TextView
    android:layout_width="200dp"
    android:layout_height="40dp"
    android:background="#6ff9c1"
    android:layout_alignRight="@id/center"/>
1
2
3
4
5

image-20201124152727702

# 基础属性

# 1.位置属性

以下的属性值为true或者false

android:layout_centerHrizontal        水平居中
android:layout_centerVertical         垂直居中
android:layout_centerInparent         相对于父元素完全居中
android:layout_alignParentBottom      贴紧父元素的下边缘
android:layout_alignParentLeft        贴紧父元素的左边缘
android:layout_alignParentRight       贴紧父元素的右边缘
android:layout_alignParentTop         贴紧父元素的上边缘
android:layout_alignWithParentIfMissing 如果对应的兄弟元素找不到的话就以父元素做参照物
1
2
3
4
5
6
7
8

# 2.尺寸属性

以下属性值为具体的像素值,如30dip,40px

android:layout_marginBottom     离某元素底边缘的距离
android:layout_marginLeft       离某元素左边缘的距离
android:layout_marginRight      离某元素右边缘的距离
android:layout_marginTop        离某元素上边缘的距离
1
2
3
4

# 3.id引用属性

以下属性值必须为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    本元素的右边缘和某元素的的右边缘对齐
1
2
3
4
5
6
7
8


# RelativeLayout的坑

image-20210315194914671

在使用ListView时,如果使用相对布局,不加android:layout_above="@id/"则会看不到最后一条item


# 页面Demo

image-20201124164558075

如何确定宽高的属性值?

  • 拆谁就将谁设置为0dp

【第一步】:先分为上下两部分水平布局,由于是上下分,所以宽不变

  • android:layout_width="match_parent"
  • android:layout_height="0dp"

【第二步】:下部分分为三个相对布局,高不变,拆分宽

  • android:layout_width="0dp"
  • android:layout_height="match_parent"

【第三部】:上面分为左右两大部分,左侧为相对布局,右侧为一个大整体,大整体下分两个相对布局

[3.1] 两大部分-高不变,拆分宽

  • android:layout_width="0dp"
  • android:layout_height="match_parent"

[3.2] 右侧两个相对垂直布局,宽不变,拆分高

  • android:layout_width="match_parent"
  • android:layout_height="0dp"

View_LinearLayout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!--上半部分-->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="horizontal"
        android:layout_weight="2">
        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="#DAE8FC"
            android:layout_weight="2">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@mipmap/icon_3d"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:text="速度与激情10"
                android:textSize="30sp"
                android:textAlignment="center"
                android:textColor="#666666"
                android:background="#FFFFFF"
                android:layout_alignParentBottom="true"/>
        </RelativeLayout>
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical">
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:background="#79d2d2"
                android:layout_weight="1">
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@mipmap/icon_imax2d"/>
            </RelativeLayout>
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="#6ff9c1">
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@mipmap/icon_imax3d"/>
            </RelativeLayout>
        </LinearLayout>
    </LinearLayout>

    <!--下半部分-->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="horizontal"
        android:background="#D5E8D4"
        android:layout_weight="1">
        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="#ccff99"
            android:layout_weight="1">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@mipmap/icon_4d"/>
        </RelativeLayout>
        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="#99f158"
            android:layout_weight="1">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@mipmap/icon_imax3d"/>
        </RelativeLayout>
        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="#02b340"
            android:layout_weight="1">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@mipmap/icon_4d"/>
        </RelativeLayout>
    </LinearLayout>

</LinearLayout>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
编辑 (opens new window)
上次更新: 2022/10/25, 23:13:03
线性布局LinearLayout
ConstraintLaout使用

← 线性布局LinearLayout ConstraintLaout使用→

最近更新
01
匿名内部类
10-08
02
函数式接口
10-08
03
ARouter-Kotlin踩坑
10-05
更多文章>
Theme by Vdoing | Copyright © 2021-2023 iqqcode | MIT License | 备案号-京ICP备2021028793号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×