RelativeLayout初步了解

AI权益加码!Claude Code、Cursor等20+工具免费用! 购周边限时加赠Coding Plan Lite,畅享主流AI工具!学习进阶更高效! 阅读详情
特点:按组件的相对位置来多布局,如某个组件在某个组件的上下左右。

常用属性:
XML属性 描述
android:layout_above 指定该组件在某个组件上方
android:layout_below 指定该组件在某个组件下方
android:layout_toLeftOf 指定该组件在某个组件左边
android:layout_toRightOf 指定该组件在某个组件的右边
android:layout_centerInParent 指定该组件在父组件的中间
android:layout_centerVertical 垂直居中
android:layout_centralHorizontal 水平居中
android:layout_alignRight 指定这个组件和哪个组件左边对齐
android:layout_alignLet 指定这个组件和哪个组件右边对齐
android:layout_alignBottom 指定这个组件和哪个组件底端对齐
android:layout_alignTop 指定这个组件和哪个组件顶端对齐
android:layout_alignParentRight 用于指定该组件是否与父控件右端对齐
android:layout_alignParentLeft 指定该组件是否与父控件左端对齐
android:layout_alignParentTop 指定该组件是否与父控件顶端对齐
android:layout_alignParentDown 指定该组件是否与父控件下面对齐
代码示意:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
 
<Button
android:id="@+id/btn_center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="中" />
 
<Button
android:id="@+id/btn_up"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/btn_center"
android:layout_alignLeft="@+id/btn_center"
android:text="上" />
 
<Button
android:id="@+id/btn_down"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_center"
android:layout_centerHorizontal="true"
android:text="下" />
 
<Button
android:id="@+id/btn_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/btn_center"
android:text="左" />
 
<Button
android:id="@+id/btn_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btn_center"
android:layout_toRightOf="@+id/btn_center"
android:text="右" />
 
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
 
<Button
android:id="@+id/btn_center1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="中" />
 
<Button
android:id="@+id/btn_left1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/btn_center1"
android:text="左" />
 
<Button
android:id="@+id/btn_right1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btn_left1"
android:layout_alignParentRight="true"
android:text="右" />
 
<Button
android:id="@+id/btn_left2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_center1"
android:text="左" />
 
<Button
android:id="@+id/btn_right2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btn_left2"
android:layout_alignParentRight="true"
android:text="右" />
 
</RelativeLayout>
RelativeLayout源码解析 为什么学习 一直对RelativeLayout如何把xml中的属性转换成子view的真实坐标比较感兴趣,并且网上对RelativeLayout这部分资料比较少,希望将自己对RelativeLayout这部分源码理解写出来,跟大家分享一下。什么是RelativeLayout 中文名叫相对布局,可以指定内部子view之间的相互关系,从而可以绘制出更精确的UI 但是相比于LinearLayout,R 阅读详情

相关推荐

RelativeLayout(相对布局)用法实例讲解

本节引言 LinearLayout也是我们用的比较多的一个布局,我们更多的时候更钟情于他的weight(权重)属性,等比例划分,对屏幕适配还是帮助蛮大的;但是使用LinearLayout的时候也有一个问题,就是当界面比较复杂的时候,需要嵌套多层的 LinearLayout,这样就会降低UI Render的效率(渲染速度),而且如果是listview或者GridView上的 item,效率会更低,另外太多层LinearLayout嵌套会占用更多的系统资源,还有可能引发stackoverflow; 但是如果我们使用RelativeLayout的话,可能仅仅需要一层就可以完成了,以父容器或者兄弟组件

熟悉AndroidAPI系列8——RelativeLayout综合练习

组件布局最好通过一个兄弟组件或者父组件确定其位置坐标。 gravity属性,能容在组件内部的位置 EditText中的提示属性hint,以及如何设置密码的输入格式 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools...

ajws21489的博客 160

Android开发重点难点1:RelativeLayout(相对布局)详解

前言   啦啦啦~博主又推出了一个新的系列啦~   之前的Android开发系列主要以完成实验的过程为主,经常会综合许多知识来写,所以难免会有知识点的交杂,给人一种混乱的感觉。   所以博主推出“重点难点”系列,将博主在完成实验的过程中遇到的重、难点或者出现问题较多的地方写出来与大家分享,使大家能更好的学习Android开发的相关知识(我知道我是好人,大家不用夸奖我啦~手动滑稽)。 ...

weixin_30725315的博客 2872

RelativeLayout

RelativeLayout(相对布局)的特点 ;灵活性强,属性复杂 第一类:属性用true或false表示 (1)居中 android:layout_centerHrizontal  若为true,水平居中 android:layout_centerVertical  若为true,垂直居中  android:layout_centerInparent  若为tru

路在脚下 742

RelativeLayout的基本对齐方式

RelativeLayout(相对布局)中,每个组件都可以通过ID来指定相对于其它组件或者父组件的位置。1、通过ID来指定相对于其它组件的位置,组件之间的相对位置关系设置如下: android:layout_above 将组件放在指定ID组件的上方 android:layout_below 将组件放在指定ID组件的下方 android:layout_toRig

BASIC_DU的博客 2万+

深入了解 Android 中的 RelativeLayout 布局

深入了解 Android 中的 RelativeLayout 布局

一起coding,一起嗨。 1591

RelativeLayout布局

RelativeLayout布局是相对布局,如果RelativeLayout中再包含两个RelativeLayout,不会像LinearLayout一样,宽高一样的话会重叠在一起 将红色布局放到右上角 常见属性 根据父容器定位 layout_alignParentLeft 左对齐 layout_alignParentRight 右对齐 layout_alignParentTop 顶部对齐 layout_alignParentBottom 底部对齐 layout_centerHorizontal 水平居中

qq_44610809的博客 4745

android relativelayout属性总结

------------指定偏移量 1.android:paddingleft(paddingright/paddingtop/paddingbottom):控件内部内容(如文本距离该控件的边距); 2.android:paddingstart(paddingend):左右对齐时使用。左对齐时:paddingstart对应paddingleft;paddingend对应paddingrig

kimifdw的专栏 5467

Android Layout 之 RelativeLayout

Android Layout 之 RelativeLayout  使用 AbsoluteLayout 可以直接指定其子 View  的绝对位置, 这种布局方式虽然简单,但是不够灵活。比如在一个程序中,按钮2 位于 按钮1 的下方且和 按钮1 左对齐,我们可以使用指定两个按钮的绝对位置的方式布局,但是当布局完成后,由于某些原因,这两个按钮需要相左平移一些距离以便在父 View 右边留出一些空白区

Android_G的专栏 1万+

Android常用布局控件之RelativeLayout

        我们使用LinearLayout和TableLayout可以满足开发应用程序界面基本的要求。但是有时候实现界面的时候不够灵活,我们还可以使用另外一种控件RelativeLayoutRelativeLayout是一种相对布局的控件,这个容器内部的子元素们可以使用彼此之间的相对位置或者和容器间的相对位置来进行定位,类似于网页设计中的CSS。在指定控件的位置时,我们需要指定这个控件与其它控件之间的相对位置关系,比如说与另一个控件的左边对齐,右对齐,位于另一个控件的上方,下方等等。一个控件可以指定

ITCEOjingying的专栏 1万+

RelativeLayout在flutter体现

android flutter FrameLayout/RelativeLayout ---------------------------------- Stack <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:an...

涂文远的博客 1388

RelativeLayout(相对布局)

RelativeLayout 教程摘要 RelativeLayoutAndroid 中通过相对定位排列子 View 的布局方式,支持基于兄弟 View 或父容器的位置关系(如居中对齐、上下排列等)。它减少了布局嵌套但性能低于 ConstraintLayout,Google 推荐后者替代。核心属性包括 layout_below、layout_alignParentTop 等,需为 View 设置 ID 以便引用。教程提供了 XML 和代码两种实现方式,示例展示计数器界面布局,并强调动态代码中通过 add

m0_57545130的博客 1352

android relativelayout 底部,Android RelativeLayout 属性

// 相对于给定ID控件Android:layout_above 将该控件的底部置于给定ID的控件之上;android:layout_below 将该控件的底部置于给定ID的控件之下;android:layout_toLeftOf 将该控件的右边缘与给定ID的控件左边缘对齐;android:layout_toRightOf 将该控件的左边缘与给定ID的控件右边缘对齐;android:lay...

weixin_30899023的博客 750

RelativeLayout

本讲内容:常用Layout介绍: AbsoluteLayout和RelativeLayout 点此下载:Android学习指南第六讲源代码 3、绝对布局 AbsoluteLayout 绝对定位AbsoluteLayout,又可以叫做坐标布局,可以直接指定子元素的绝对位置,这种布局简单直接,直观性强,但是由于手机屏幕尺寸差别比较大,使用绝对定位的适应性会比较差。 下面我们举一

zhaohc_nj的专栏 892

RelativeLayout 原理浅析

本文基于api level 26 的代码,简单描述一下 RelativeLayout 的内部工作原理。

黄金夫的博客 1319

Activity的布局初步(三)相对布局【RelativeLayout

相对布局,在这个容器内的子元素们可以通过彼此之间的位置来相互定位,或者与其父控件容器进行相互定位。注意:不能在RelativeLayout容器本身的大小和他的子元素之间产生循环依赖,例如,不能给RelativeLayout容器的高设置成为WRAP_CONTENT属性,而给子元素的高设置成为ALIGN_PARENT_BOTTOM属性。   循环依赖理解起来就是,容器本身的WRAP_CONTEN

LO/QI 1030
上一篇: JSON认识
下一篇: LinearLayout初步了解
Yo_Hack
博客等级 码龄12年 2粉丝 44原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值