【约束布局】ConstraintLayout 组件可见性 View.GONE 处理 与 Margin 属性

Android开发核心:从LinearLayout到ConstraintLayout的布局选型与性能优化实战 在Android应用开发中,用户界面(UI)的构建是基础且关键的一环,其核心在于布局系统。布局系统通过一套XML描述语言,定义了视图(View)在屏幕上的位置、尺寸和排列关系,是应用视觉呈现和交互体验的基石。从原理上讲,Android系统在运行时解析这些布局文件,执行测量(Measure)、布局(Layout)和绘制(Draw)流程,最终将UI渲染到屏幕上。掌握高效的布局技术,对于构建流畅、美观且适配性强的应用具有重要价值。常见的应用场景包括构建列表项、实现复杂响应式界面以及协调多视图交互。本文将聚焦于An 阅读详情

很久没写文章了,但是针对今天这篇文章还是值得记录下,供大家参考学习,目前布局中用ConstraintLayout(简称cons)应该很常见了,至于为什么要用这个布局,我想大家用过的都知道cons没有出现之前稍微复杂点的界面就各种嵌套,对于维护,修改也不是很方便。cons它作用就是控件与控件直接约束,不需要额外的嵌套。好了说下今天的正题:

1.首先了解下cons中的Margin属性:

cons中用Margin属性必须有个条件就是约束,比如你要设置marginStart=15dp.那么你必须有个横向约束,要设置margintop,必须要有竖向约束。不然就算你设置了值也不会生效的,当然这是约束布局的基本原则。
2.当组件设置View.GONE时,Margin属性以及goneMargin属性的作用:

这个就是今天的重点,前面讲了margin属性的设置,现在模拟一个场景假如有A,B,C三个控件,依次ABC顺序:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@mipmap/shadow">

    <TextView
        android:id="@+id/textview_a"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="A"
        android:textColor="@color/black"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textview_b"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="B"
        android:textColor="@color/black"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@id/textview_c"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textview_a" />

    <TextView
        android:id="@+id/textview_c"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:text="C"
        android:textColor="@color/black"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textview_b" />
</androidx.constraintlayout.widget.ConstraintLayout>

每个都设置了10dp间隔,现在C在底部,并且距离底部有10dp,(我们假如设计图就是这么设计的),现在需要根据数据控制C是否显示,假如C不显示那么B是什么效果呐,改下布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@mipmap/shadow">

    <TextView
        android:id="@+id/textview_a"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="A"
        android:textColor="@color/black"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textview_b"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="B"
        android:textColor="@color/black"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@id/textview_c"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textview_a" />

    <TextView
        android:id="@+id/textview_c"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:text="C"
        android:textColor="@color/black"
        android:visibility="gone"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textview_b" />
</androidx.constraintlayout.widget.ConstraintLayout>

很明显B挨着底部了,没有10dp间距,这样跟设计图不符,这时候有朋友在想我给B设置个marginBottom=10dp不就行了,雀氏在C控件不显示情况下没有问题,C控件显示是不是间距就是20dp了?

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@mipmap/shadow">

    <TextView
        android:id="@+id/textview_a"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="A"
        android:textColor="@color/black"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textview_b"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="B"
        android:textColor="@color/black"
        android:textSize="18sp"
        android:textStyle="bold"
        android:layout_marginBottom="10dp"
        app:layout_constraintBottom_toTopOf="@id/textview_c"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textview_a" />

    <TextView
        android:id="@+id/textview_c"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:text="C"
        android:textColor="@color/black"

        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textview_b" />
</androidx.constraintlayout.widget.ConstraintLayout>

等会,抽支烟冷静下。这时候就有了goneMargin了,顾名思义,就是在约束布局中对于控件设置了gone时该属性起效。我们先改下代码看下效果:
 

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@mipmap/shadow">

    <TextView
        android:id="@+id/textview_a"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="A"
        android:textColor="@color/black"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textview_b"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="B"
        android:textColor="@color/black"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_goneMarginBottom="10dp"
        app:layout_constraintBottom_toTopOf="@id/textview_c"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textview_a" />

    <TextView
        android:id="@+id/textview_c"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:text="C"
        android:textColor="@color/black"
        android:textSize="18sp"
        android:textStyle="bold"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textview_b" />
</androidx.constraintlayout.widget.ConstraintLayout>

是不是就有了,这个属性是针对可见属性设置的,如果约束的控件不可见(C)该属性起效。

有人在想如果这时候c可见了会不会跟上面一样20dp间距,骚年别慌,看下面代码就知道了
 

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@mipmap/shadow">

    <TextView
        android:id="@+id/textview_a"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="A"
        android:textColor="@color/black"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textview_b"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="B"
        android:textColor="@color/black"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_goneMarginBottom="10dp"
        app:layout_constraintBottom_toTopOf="@id/textview_c"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textview_a" />

    <TextView
        android:id="@+id/textview_c"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:text="C"
        android:textColor="@color/black"
        android:textSize="18sp"
        android:textStyle="bold"

        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textview_b" />
</androidx.constraintlayout.widget.ConstraintLayout>
总结:cons布局功能虽然强大,但是使用过程中还是有许多细节地方需要注意的,比如ScroolView嵌套cons,cons高度最好建议设置成match_partent(如果cons中还有rv显示不全的话也设置match_partent),此外对于goneMarin属性的其他方向间距是类似的(说明:不是说只能用goneMargin才能实现上面效果)。如果此篇文章有帮助到你,就请给个赞吧!

Android RelativeLayout核心机制与实战优化指南 在Android开发中,布局系统是构建用户界面的基础,它决定了UI组件的排列与定位方式。RelativeLayout作为Android经典布局之一,其核心原理是基于视图间的相对位置关系进行仲裁与测量,通过依赖规则描述组件与父容器或兄弟视图的空间关联。这种设计在中等复杂度界面构建中提供了灵活性,能有效减少嵌套层级,提升代码可读性。从技术价值看,掌握RelativeLayout有助于理解Android布局体系的设计哲学,并为维护存量项目提供必要基础。在实际应用场景中,它特别适合处理表单布局、信息卡片及需要视图层 阅读详情

相关推荐

对比实验系列:Efficientdet环境配置及训练个人数据集

可以通过下方链接下载Efficientdet源码。

Yayisheng的博客 4585

【约束布局】ConstraintLayout 组件可见性 View.GONE 处理 与 Margin 属性 ( 约束布局可见性处理 | goneMargin 属性 )

相对定位 Margin 属性 可见性改变后的行为处理 ( Visibility Behavior ) 组件设置 GONE 属性示例 被 约束到不可见组件 后 Margin 属性 表现 goneMargin 属性示例

让 学习 成为一种 习惯 ( 韩曙亮 の 技术博客 ) 1万+

Jetson平台XDMA PCIe驱动安装与测试全指南

本文提供了一份在NVIDIA Jetson平台上安装与测试Xilinx FPGA XDMA PCIe驱动的完整指南。详细阐述了驱动作为高速DMA引擎的核心作用,并逐步讲解了从环境准备、驱动编译、内核模块加载到实际数据传输测试的全过程。内容涵盖常见问题排查与性能优化建议,帮助开发者实现Jetson与FPGA间的高速数据通信,适用于自动驾驶、工业视觉等需要异构计算的应用场景。

weixin_29196315的博客 526

Android——ConstraintLayout(约束布局)

约束布局ConstraintLayout 是一个ViewGroup,可以在Api9以上的Android系统使用它,它的出现主要是为了解决布局嵌套过多的问题,以灵活的方式定位和调整小部件。从 Android Studio 2.3 起,官方的模板默认使用 ConstraintLayout。

The_onion的博客 1万+

UniApp消息推送进阶:不用插件实现动态语音播报(含保活策略)

本文详细介绍了在UniApp中实现动态语音播报的进阶方案,无需依赖第三方插件。核心采用透传消息传递动态数据,结合本地TTS引擎合成语音,并针对不同安卓厂商提供了深度通道配置与差异化保活策略,有效解决了App退至后台或被清理后语音播报失效的难题。

weixin_29224309的博客 486

转录组测序(RNA-seq)的数据如何分析?

经过富集分析后,我们就可以从富集到的GO term或者Pathway中寻找关键基因,通过一个GO term和Pathway里面的基因往往小于100个,至此我们就又将上千条基因的范围缩小到了100条以内。对GO term或者Pathway里面的基因进行范围的缩小也有两个方法,一种方法是根据基因的注释或者文献来找到GO term或者Pathway里面的关键基因(2-3个),另一种方法需要有相应的表型数据,与GO term或者Pathway里面基因进行相关性分析,找出与表型高度相关的基因。

weixin_55372631的博客 1万+

ConstraintLayout约束布局属性

ConstraintLayout属性 相对定位 layout_constraintLeft_toLeftOf layout_constraintLeft_toRightOf layout_constraintRight_toLeftOf layout_constraintRight_toRightOf layout_constraintTop_toTopOf layout_constraintTo...

shenglong0210的博客 1144

【PointNet++】制作点云数据集使用CloudCompare进行标注

使用PointNet++复现成功后,需要训练自己的数据集。于是使用cloudcompare参照ShapeNet的数据集格式去制作,最终保存的txt文件格式为“xyz+rgb+label”,一共7列。 自己按此制作好的数据集已成功训练及推测,于是记录下来让好兄弟好姐妹们少踩坑多吃肉。

纵马踏花向自由 5714

Android开发之ConstraintLayout代码动态控制约束布局-已踩坑(跟Compose的约束布局基本一个套路)

需求如上图我需要手动点击按钮改变文字的位置,在约束布局中唯一快速想到的我就是动态设置。兄弟们上面的坑我已经踩完了,都已注释,具体可以看注释,在此非常感谢二位大佬无私分享。

xiayiye5的博客 5086

【CASS精品教程】006:CASS7.1 道路设计参数文件打开无响应,提示roadpara解决办法,权威解决办法汇总

在用CASS7.1 软件进行断面法土方量计算,在生成断面图后,需要进行道路设计参数文件的设计,打开断面设计参数文件的时候,无响应,提示roadpara,如下所示: 道路设计参数文件 解决办法有三种 第一种方法:Win+R (1)64位系统:输入regsvr32 C:\Windows\SysWOW64\CellCtrl5.ocx,回车 (2)32位系统输入regsvr32 C:...

智慧空间实验室 1万+

OD图 arcgis曲线制作插件 od图曲线 路径图曲线制作插件 航线图曲线制作插件

在ArcGIS 10.2 中导入节点数据,打开插件,选择输入起始节点和目标节点的节点文件Point,以Name字段(地名)为连接线的起始字段和目标字段,(Input Table File)选择连接关系表格SDLine.csv,(Origin Column 和 Destination Column)为起始节点和目标节点,(Output Path)为输出路径,应选择.gdb数据库路径,否则容易报错,(Chord Coefficient)为连线的曲率,数值越大,曲率越高。运行环境:ArcGIS10.2。...

JData_Engineer的博客 2670

android -------- ConstraintLayout Group和goneMargin(五)

前面的文章 ConstraintLayout 介绍(一) ConstraintLayout 约束属性(二) ConstraintLayout 宽高比和偏移量比(三) ConstraintLayout Guideline和Barrier(四) 此博文主要讲解: Group和goneMargin 1:Group 在开发中,有时候需要同时隐藏或者显示多个控件,用Group就可以很好的实现,...

切切歆语的博客 5989

Python 爬虫实战:Boss直聘 Python 岗位招聘数据爬取

本文介绍了一个Python爬虫项目,用于抓取Boss直聘平台的Python岗位招聘数据并进行可视化分析。项目采用requests库发送POST请求,通过抓包分析获取JSON接口,绕过HTML解析直接获取结构化数据。主要内容包括: 接口分析:找到关键POST接口,解析请求头和JSON参数格式 环境准备:安装requests/pandas/matplotlib/pyecharts等依赖库 核心实现: 手动获取登录Cookie 构建请求头和城市代码映射 实现分页抓取职位列表数据 项目特色在于直接解析JSON接口而

u014481728的博客 296

Android 几种布局设置Gone时注意事项

Android 几种布局设置Gone时注意事项需要实现的功能RelativeLayout相对布局设置goneLinearLayout线性布局设置GoneConstraintLayout约束布局设置Gone总结 需要实现的功能 在Android ui 中我们需要经常实现这种样式的布局,大家拿到UI设计的图稿中,第一反应会去想怎么做,怎么实现。 上图中比较常见的布局可以用 LinearLayout(线性) RelativeLayout (相对) 以及Google推荐的ConstraintLayout (约束

漂泊在猿海 2470

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

ConstraintLayout 属性详解 和Chain的使用

下面是有几位Android行业大佬对应上方技术点整理的一些进阶资料。希望能够帮助到大家提升技术高级UI,自定义ViewUI这块知识是现今使用者最多的。当年火爆一时的Android入门培训,学会这小块知识就能随便找到不错的工作了。不过很显然现在远远不够了,拒绝无休止的CV,亲自去项目实战,读源码,研究原理吧!《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门即可获取!漫长,而且极易碰到天花板技术停滞不前!**

2401_84121627的博客 570

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

ConstraintLayout 属性详解 和Chain的使用,android高级面试题2024

1(如* 2(如现在也是类似的,只不过除了以上两种约束,还多了一种* 3Guideline其实关于和Guideline的约束,也可以理解成约束1,因为Guideline其实就是一个在屏幕上不显示的View罢了。稍后讲到Guideline会带大家看看它巨简单的源码。下面开始正文,开始属性的讲解相对定位 (Relative positioning)这一节的属性和相对布局的很像,值得注意的是参数取值是"parent"属性都形如这里我的理解,里的XXX代表是这个子控件),而toYYYOf里的YYY。

m0_58269520的博客 1222

使用 ConstraintLayout 构建自适应界面

ConstraintLayout是Android中用于构建复杂布局的视图组,它通过扁平化视图层次结构提升性能。相比RelativeLayout,它更灵活且与布局编辑器深度集成,支持可视化拖拽设计。使用ConstraintLayout需要为视图定义水平和垂直约束条件,包括与其他视图或父布局的对齐关系。常用属性分为相对位置约束(如layout_constraintLeft_toLeftOf)和边距设置(如layout_marginTop)。该布局方案既能减少嵌套层级,又能通过XML或可视化工具灵活控制视图位置关

u012514113的专栏 839

Jetpack Compose 约束布局实战:ConstraintLayout 核心 API 与调试

在 Android 声明式 UI 开发中,布局设计直接影响界面性能与代码可维护性。传统的线性布局(Column、Row)在处理复杂控件关系时容易产生深层嵌套,增加测量与组合成本。约束布局(ConstraintLayout)通过描述控件间的相对位置关系,将嵌套层级转化为平直的约束网络,从而优化布局性能并提升可读性。作为一种成熟的 Android 布局方案,它支持 Guideline、Barrier、链式约束等高级能力,适用于动态文本对齐、比例定位和复杂相对布局等场景。理解约束布局的适用边界与调试方法,是工程实

weixin_33827590的博客 379

Android布局开发全解析:从LinearLayout到ConstraintLayout的实战与优化

在Android应用开发中,用户界面(UI)的构建是基础且核心的环节,其关键在于理解布局(Layout)系统的工作原理。布局作为视图的容器和排列规则,通过测量(Measure)、布局(Layout)、绘制(Draw)流程,将视图树渲染到屏幕上,直接影响应用的性能与用户体验。掌握高效的布局技术,能够解决多屏幕适配、渲染性能优化等工程难题,对于构建流畅、稳定的应用至关重要。本文聚焦于Android开发中的布局管理器,深入剖析LinearLayout、RelativeLayout与现代主力ConstraintLa

congdi7904的博客 542

Compose中ConstraintLayout布局优化与实战技巧

ConstraintLayout作为现代UI开发的核心布局方案,通过声明式API实现了高效的组件定位。其单次测量机制能显著提升复杂界面的渲染性能,特别适合处理动态内容和多设备适配场景。在Jetpack Compose中,ConstraintLayout通过屏障(Barrier)和引导线(Guideline)等高级功能,解决了传统布局嵌套过深导致的性能问题。开发者需要注意避免过度约束,合理选择测量策略,同时结合动画API实现流畅交互。该技术已广泛应用于表单生成、多语言适配等场景,是构建响应式Android应用

weixin_30265103的博客 362

ConstraintLayout核心原理与生产级实战指南

ConstraintLayout是Android中替代嵌套布局的声明式约束系统,其本质是通过线性方程组求解控件位置与尺寸,在满足所有约束前提下最小化布局偏差。相比RelativeLayout的测量循环依赖和LinearLayout的weight双遍历机制,它以单次测量、可编程坐标系(四向/基线/圆心/百分比)实现更高性能与精准适配。技术价值体现在滑动流畅性提升、多屏像素级还原、动态内容自适应(如Barrier响应文字行数变化)及复杂表单可控缩放。典型应用场景包括底部导航链式分布、聊天气泡左右偏移、折叠屏双列

weixin_30872157的博客 409

ConstraintLayout学习

前言 在了解学习ConstraintLayout布局之前,开发中布局一个页面习惯LinearLayout和RelativeLayout一通嵌套完成布局。但是一个项目的优化中是包括布局优化的,多层的嵌套会影响到页面的绘制,性能开销也会很大。我们总不能自己创造出问题然后再去优化吧,加上升级as后创建一个Activity的默认根布局是ConstraintLayout,既然是官方推崇的,所以就去了解学习了下。在写这篇文章前已经在项目中尝试使用了ConstraintLayout,基本上每个页面都可以一个层级去布局出

weixin_37177125的博客 577

Android XML布局开发指南:从基础到性能优化

XML布局是Android UI开发的核心技术,通过声明式语法实现界面与逻辑的分离。其工作原理是将视图层级结构转化为View对象树,具有可视化编辑、多设备适配和维护便捷等技术优势。在工程实践中,LinearLayout、RelativeLayout和ConstraintLayout是最常用的三种布局方式,其中ConstraintLayout凭借其扁平化结构和约束关系,能有效解决嵌套层级过深导致的性能问题。热门的布局复用技术如include标签和ViewStub,配合尺寸限定符等适配方案,能够显著提升开发效率

weixin_30437847的博客 465

Android RelativeLayout相对布局深度解析:核心属性、实战案例与性能优化

在Android应用开发中,UI布局是构建用户界面的基础。RelativeLayout(相对布局)通过视图间的相对位置关系进行定位,其声明式的语法允许开发者直观地描述元素间的空间关系,如对齐、居中和参照。这种布局方式的核心技术价值在于能够以简洁的XML代码实现非线性的复杂界面,有效减少嵌套层级,提升代码可读性。在适配多种屏幕尺寸、构建用户信息卡片等具体应用场景中,RelativeLayout展现出独特的灵活性。尽管面临性能考量(如两次测量过程),但通过理解其核心属性、避免循环依赖等常见陷阱,并掌握扁平化布局

weixin_34253126的博客 340
上一篇: android中使用@IntDef @StringDef代替ENUM
下一篇: Android使用Eventbus 3+意想不到的坑(全局监听事件并弹框)
ZyClient
博客等级 码龄10年 29粉丝 60原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值