笔记四:常用布局:相对布局——RelativeLayout
引言在上一节中我们对LinearLayout进行了详细的解析LinearLayout也是我们 用的比较多的一个布局,我们更多的时候更钟情于他的weight(权重)属性等比例划分对屏幕适配还是 帮助蛮大的;但是使用LinearLayout的时候也有一个问题就是当界面比较复杂的时候需要嵌套多层的 LinearLayout,这样就会降低UI Render的效率(渲染速度),而且如果是listview或者GridView上的 item,效率会更低,另外太多层LinearLayout嵌套会占用更多的系统资源,还有可能引发stackoverflow; 但是如果我们使用RelativeLayout的话,可能仅仅需要一层就可以完成了,以父容器或者兄弟组件参考margin padding就可以设置组件的显示位置,是比较方便的!当然,也不是绝对的,具体问题具体分析吧! 总结就是:尽量使用RelativeLayout LinearLayout的weight属性搭配使用吧1.核心属性图2.父容器定位属性示意图3.根据兄弟组件定位恩,先说下什么是兄弟组件吧,所谓的兄弟组件就是处于同一层次容器的组件,如图图中的组件1,2就是兄弟组件了,而组件3与组件1或组件2并不是兄弟组件,所以组件3不能通过 组件1或2来进行定位,比如layout_toleftof 组件1这样是会报错的切记 关于这个兄弟组件定位的最经典例子就是梅花布局了,下面代码实现下:运行效果图实现代码RelativeLayout xmlns:androidhttp://schemas.android.com/apk/res/android xmlns:toolshttp://schemas.android.com/tools android:idid/RelativeLayout1 android:layout_widthmatch_parent android:layout_heightmatch_parent !-- 这个是在容器中央的 -- ImageView android:idid/img1 android:layout_width80dp android:layout_height80dp android:layout_centerInParenttrue android:srcdrawable/pic1/ !-- 在中间图片的左边 -- ImageView android:idid/img2 android:layout_width80dp android:layout_height80dp android:layout_toLeftOfid/img1 android:layout_centerVerticaltrue android:srcdrawable/pic2/ !-- 在中间图片的右边 -- ImageView android:idid/img3 android:layout_width80dp android:layout_height80dp android:layout_toRightOfid/img1 android:layout_centerVerticaltrue android:srcdrawable/pic3/ !-- 在中间图片的上面-- ImageView android:idid/img4 android:layout_width80dp android:layout_height80dp android:layout_aboveid/img1 android:layout_centerHorizontaltrue android:srcdrawable/pic4/ !-- 在中间图片的下面 -- ImageView android:idid/img5 android:layout_width80dp android:layout_height80dp android:layout_belowid/img1 android:layout_centerHorizontaltrue android:srcdrawable/pic5/ /RelativeLayout4.margin与padding的区别初学者对于这两个属性可能会有一点混淆这里区分下 首先margin代表的是偏移,比如marginleft 5dp表示组件离容器左边缘偏移5dp; 而padding代表的则是填充,而填充的对象针对的是组件中的元素,比如TextView中的文字 比如为TextView设置paddingleft “5dp”,则是在组件里的元素的左边填充5dp的空间 margin针对的是容器中的组件而padding针对的是组件中的元素要区分开来 下面通过简单的代码演示两者的区别:比较示例代码如下RelativeLayout xmlns:androidhttp://schemas.android.com/apk/res/android xmlns:toolshttp://schemas.android.com/tools android:layout_widthmatch_parent android:layout_heightmatch_parent android:paddingBottomdimen/activity_vertical_margin android:paddingLeftdimen/activity_horizontal_margin android:paddingRightdimen/activity_horizontal_margin android:paddingTopdimen/activity_vertical_margin tools:context.MainActivity Button android:idid/btn1 android:layout_heightwrap_content android:layout_widthwrap_content android:textButton/ Button android:paddingLeft100dp android:layout_heightwrap_content android:layout_widthwrap_content android:textButton android:layout_toRightOfid/btn1/ Button android:idid/btn2 android:layout_heightwrap_content android:layout_widthwrap_content android:textButton android:layout_alignParentBottomtrue/ Button android:layout_marginLeft100dp android:layout_heightwrap_content android:layout_widthwrap_content android:textButton android:layout_toRightOfid/btn2 android:layout_alignParentBottomtrue/ /RelativeLayout运行效果图比较5.很常用的一点:margin可以设置为负数相信很多朋友都不知道一点吧平时我们设置margin的时候都习惯了是正数的, 其实是可以用负数的,下面写个简单的程序演示下吧,模拟进入软件后,弹出广告 页面的,右上角的cancle按钮的margin则是使用负数的效果图如下:贴出的广告Activity的布局代码吧,当然,如果你对这个有兴趣的话可以下下demo, 因为仅仅是实现效果,所以代码会有些粗糙RelativeLayout xmlns:androidhttp://schemas.android.com/apk/res/android xmlns:toolshttp://schemas.android.com/tools android:layout_widthmatch_parent android:layout_heightmatch_parent tools:contextcom.jay.example.relativelayoutdemo.MainActivity android:background#00CCCCFF ImageView android:idid/imgBack android:layout_width200dp android:layout_height200dp android:layout_centerInParenttrue android:backgrounddrawable/myicon / ImageView android:idid/imgCancle android:layout_width28dp android:layout_height28dp android:layout_alignRightid/imgBack android:layout_alignTopid/imgBack android:backgrounddrawable/cancel android:layout_marginTop-15dp android:layout_marginRight-10dp / /RelativeLayout

相关新闻

如何快速找到并安装最适合的用户脚本:Greasy Fork实用指南

如何快速找到并安装最适合的用户脚本:Greasy Fork实用指南

如何快速找到并安装最适合的用户脚本:Greasy Fork实用指南 【免费下载链接】greasyfork An online repository of user scripts. 项目地址: https://gitcode.com/gh_mirrors/gr/greasyfork Greasy Fork是一个功能强大的在线用户脚本仓库,为用户提…

2026/10/30 2:09:22 阅读更多 →
问题解决方案之VC++

问题解决方案之VC++

文|Seraph本文档主要记录VC编程过程中,经常遇到的一些问题,以供大家参考。一、开发多文档形式在初始化时,使其框架以及视图最大化 框架最大化为在应用程序类的初始化函数中添加如下代码:BOOL CMDIAppDemoApp::InitInstance() …

2026/10/26 5:43:04 阅读更多 →
7天解放双手:AzurLaneAutoScript自动化脚本终极指南

7天解放双手:AzurLaneAutoScript自动化脚本终极指南

7天解放双手:AzurLaneAutoScript自动化脚本终极指南 【免费下载链接】AzurLaneAutoScript Azur Lane bot (CN/EN/JP/TW) 碧蓝航线脚本 | 无缝委托科研,全自动大世界 项目地址: https://gitcode.com/gh_mirrors/az/AzurLaneAutoScript 还在为碧蓝航…

2026/11/4 0:06:37 阅读更多 →

最新新闻

Electron桌面应用开发实战:从环境搭建到打包发布全流程

Electron桌面应用开发实战:从环境搭建到打包发布全流程

做前端这些年,我大部分时间都在跟浏览器标签页打交道,直到第一次把桌面应用跑起来,才发现原来自己一直被限制在地址栏和刷新按钮围起来的框框里。Electron这个词,最开始是在某段时间频繁出现在同事的讨论里,一个用HTML…

2026/11/3 1:09:25 阅读更多 →
滑盖智能锁T109深度解析:结构原理、安装调试与故障排查

滑盖智能锁T109深度解析:结构原理、安装调试与故障排查

简介:亚太天能T109滑盖智能锁产品说明书是一份PDF格式的官方技术文档,主要面向智能锁安装人员、终端用户及售后维护者,帮助快速掌握产品安装、配置与常见故障处理。全包仅1个PDF文件,大小4.37MB,内容结构清晰&#xff…

2026/11/3 10:57:27 阅读更多 →
PMSM弱磁控制原理与Simulink建模:高速AGV驱动系统设计指南

PMSM弱磁控制原理与Simulink建模:高速AGV驱动系统设计指南

上个月我在调试一台高速AGV的驱动系统,整车要求空载跑到8m/s,电机转速已经拉到额定点,驱动器的输出电压也顶到了母线电压极限附近。再往上踩速度指令,转速纹丝不动,三相电流反而开始震荡。那会儿我的第一反应就是&…

2026/11/3 11:35:49 阅读更多 →
C++核心知识点:从内存管理到STL的实战指南

C++核心知识点:从内存管理到STL的实战指南

C 这门语言我前前后后折腾了十几年,用它写过嵌入式驱动、做过游戏服务端,也搭过基础组件库。每次看到新人一头扎进"C 核心知识点"这个搜索词,我其实挺感慨的——这门语言的难点从来不是语法多,而是知识点之间的耦合太深…

2026/11/3 2:27:21 阅读更多 →
T109滑盖智能锁怎么装?安装调试与故障排查全攻略

T109滑盖智能锁怎么装?安装调试与故障排查全攻略

简介:亚太天能T109滑盖智能锁产品说明书以PDF格式发布,适合智能锁用户、安装人员与售后维修人员阅读。内容围绕T109系列指纹密码刷卡锁展开,先介绍前面板、后面板、锁体、机械钥匙、电池盒盖等部件名称与功能,再给出完整安装步骤&…

2026/11/3 10:47:27 阅读更多 →
LLM知识降熵:从信息垄断到理解权下放

LLM知识降熵:从信息垄断到理解权下放

你有没有过这种经历:翻开源码注释、论文摘要或者一份专业报告,每个字都认识,连在一起却像天书。想找人聊聊,身边要么没人懂,要么懂的懒得跟你解释。后来我试着把同一段内容丢给一个大语言模型,几秒钟之后&a…

2026/11/3 10:57:33 阅读更多 →

日新闻

周新闻

月新闻