文章目录
- 介绍
- 常见属性
- 根据父容器定位
- 根据兄弟组件定位
- 通用属性
- margin 设置组件与父容器的边距
- padding 设置组件内部元素的边距
- 项目结构
- 主要代码
介绍
RelativeLayout是一个相对布局,如果不指定对齐位置,都是默认相对于父容器的左上角的开始布局
常见属性
根据父容器定位
layout_alignParentLeft:左对齐layout_alignParentRight:右对齐layout_alignParentTop:顶部对齐layout_alignParentBottom:底部对齐layout_centerHorizontal:水平居中layout_centerVertical:垂直居中layout_centerInParent:中间位置
根据兄弟组件定位
layout_toLeftOf:放置于参考组件的左边layout_toRightOf:放置于参考组件的右边layout_above:放置于参考组件的上方layout_below:放置于参考组件的下方layout_alignTop:对齐参考组件的上边界layout_alignBottom:对齐参考组件的下边界layout_alignLeft:对齐参考组件的左边界layout_alignRight:对齐参考组件的右边界
通用属性
margin 设置组件与父容器的边距
layout_margin:上下左右偏移layout_marginLeft:左偏移layout_marginRight:右偏移layout_marginTop:上偏移layout_marginBottom:下偏移
padding 设置组件内部元素的边距
项目结构

主要代码
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:padding="50dp"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/rl1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:background="#ff0000" />
<!-- 这里发现如果不进行任何设置的话会将上面的布局进行一个覆盖-->
<RelativeLayout
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_toRightOf="@+id/rl1"
android:background="#00ff00" />
<RelativeLayout
android:layout_alignParentBottom="true"
android:layout_marginLeft="100dp"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#0000ff" />
</RelativeLayout>

![[足式机器人]Part5 机械设计 Ch00/01 绪论+机器结构组成与连接 ——【课程笔记】](https://img-blog.csdnimg.cn/74d59e1eaea643a8b8721b6172615477.png)

















