当前位置: 首页 > article >正文

ConstraintLayout的‘隐藏技巧’:用百分比、比例和GoneMargin搞定复杂UI适配

ConstraintLayout高级适配技巧百分比、比例与动态隐藏视图的完美解决方案在Android开发中ConstraintLayout已经成为构建复杂界面的首选布局方式。但许多开发者仅仅停留在基础使用层面未能充分发挥其强大的适配能力。本文将深入探讨三个关键技巧百分比布局、固定宽高比控制以及动态隐藏视图时的平滑过渡处理。1. 百分比布局精准控制视图尺寸百分比布局是ConstraintLayout中最实用的功能之一尤其适合需要根据屏幕尺寸动态调整的界面元素。通过layout_constraintWidth_percent和layout_constraintHeight_percent属性我们可以轻松实现这一目标。1.1 基础百分比实现假设我们需要创建一个宽度为屏幕宽度60%的按钮Button android:idid/btn_percent android:layout_width0dp android:layout_heightwrap_content app:layout_constraintStart_toStartOfparent app:layout_constraintEnd_toEndOfparent app:layout_constraintWidth_percent0.6 android:text60%宽度按钮/关键点解析必须设置宽度为0dpMATCH_CONSTRAINT需要同时定义左右约束start和end百分比值范围0-1表示相对于父容器的比例1.2 复杂百分比组合在实际项目中我们经常需要组合使用水平和垂直方向的百分比ImageView android:idid/iv_banner android:layout_width0dp android:layout_height0dp app:layout_constraintStart_toStartOfparent app:layout_constraintEnd_toEndOfparent app:layout_constraintTop_toTopOfparent app:layout_constraintWidth_percent0.9 app:layout_constraintHeight_percent0.2 android:scaleTypecenterCrop android:srcdrawable/banner/这种组合特别适合Banner图、广告位等需要保持特定比例又需要适应不同屏幕的元素。2. 固定宽高比保持视觉一致性layout_constraintDimensionRatio属性是ConstraintLayout中处理固定宽高比的利器它能够确保视图在任何尺寸下都保持预设的比例。2.1 基础比例设置创建一个1:1的正方形视图View android:idid/square_view android:layout_width0dp android:layout_height0dp app:layout_constraintStart_toStartOfparent app:layout_constraintTop_toTopOfparent app:layout_constraintDimensionRatio1:1 android:background#4CAF50/2.2 高级比例控制当只需要固定一个方向的尺寸时可以使用W或H前缀!-- 高度固定为宽度的2/3 -- ImageView android:idid/iv_avatar android:layout_width100dp android:layout_height0dp app:layout_constraintDimensionRatioH,2:3 android:scaleTypecenterCrop android:srcdrawable/avatar/ !-- 宽度固定为高度的16:9 -- FrameLayout android:idid/video_container android:layout_width0dp android:layout_height200dp app:layout_constraintDimensionRatioW,16:9/2.3 比例与百分比的组合应用结合百分比和比例属性可以创建出高度自适应的UI元素FrameLayout android:idid/card_view android:layout_width0dp android:layout_height0dp app:layout_constraintStart_toStartOfparent app:layout_constraintEnd_toEndOfparent app:layout_constraintTop_toTopOfparent app:layout_constraintWidth_percent0.8 app:layout_constraintDimensionRatioH,3:4 android:backgrounddrawable/card_bg/这种组合特别适合卡片式设计确保在不同屏幕尺寸下都能保持一致的视觉效果。3. GoneMargin处理动态隐藏视图的优雅方案当界面中的视图需要动态显示/隐藏时传统布局往往会出现突兀的空白区域。ConstraintLayout的goneMargin属性提供了完美的解决方案。3.1 基础GoneMargin使用考虑一个常见的场景标题栏左侧有返回按钮右侧有操作按钮ImageView android:idid/iv_back android:layout_width24dp android:layout_height24dp app:layout_constraintStart_toStartOfparent app:layout_constraintTop_toTopOfparent android:layout_marginStart16dp android:layout_marginTop16dp android:srcdrawable/ic_back/ TextView android:idid/tv_title android:layout_widthwrap_content android:layout_heightwrap_content app:layout_constraintTop_toTopOfid/iv_back app:layout_constraintBottom_toBottomOfid/iv_back app:layout_constraintStart_toEndOfid/iv_back app:layout_constraintEnd_toStartOfid/iv_action android:text页面标题/ ImageView android:idid/iv_action android:layout_width24dp android:layout_height24dp app:layout_constraintEnd_toEndOfparent app:layout_constraintTop_toTopOfid/iv_back android:layout_marginEnd16dp app:layout_goneMarginEnd16dp android:srcdrawable/ic_more/当iv_action设置为GONE时app:layout_goneMarginEnd16dp确保了tv_title会向右扩展到距离父视图16dp的位置而不是紧贴右侧。3.2 多视图联动处理在更复杂的场景中多个视图的隐藏可能会影响整体布局Button android:idid/btn_left android:layout_width0dp android:layout_heightwrap_content app:layout_constraintStart_toStartOfparent app:layout_constraintEnd_toStartOfid/btn_center app:layout_constraintHorizontal_weight1 android:text左按钮 app:layout_goneMarginEnd8dp/ Button android:idid/btn_center android:layout_width0dp android:layout_heightwrap_content app:layout_constraintStart_toEndOfid/btn_left app:layout_constraintEnd_toStartOfid/btn_right app:layout_constraintHorizontal_weight1 android:text中按钮 app:layout_goneMarginStart8dp app:layout_goneMarginEnd8dp/ Button android:idid/btn_right android:layout_width0dp android:layout_heightwrap_content app:layout_constraintStart_toEndOfid/btn_center app:layout_constraintEnd_toEndOfparent app:layout_constraintHorizontal_weight1 android:text右按钮 app:layout_goneMarginStart8dp/当中间按钮隐藏时左右按钮会自动调整间距保持整体布局的平衡。4. 实战案例组合应用高级技巧让我们通过一个完整的用户资料卡片案例综合运用上述技巧androidx.constraintlayout.widget.ConstraintLayout android:layout_widthmatch_parent android:layout_heightwrap_content android:padding16dp !-- 头像 - 固定比例圆形 -- ImageView android:idid/iv_avatar android:layout_width0dp android:layout_height0dp app:layout_constraintStart_toStartOfparent app:layout_constraintTop_toTopOfparent app:layout_constraintWidth_percent0.25 app:layout_constraintDimensionRatio1:1 android:scaleTypecenterCrop android:backgrounddrawable/circle_bg android:srcdrawable/avatar/ !-- 用户信息区域 -- LinearLayout android:idid/layout_info android:layout_width0dp android:layout_heightwrap_content android:orientationvertical app:layout_constraintStart_toEndOfid/iv_avatar app:layout_constraintEnd_toStartOfid/iv_verified app:layout_constraintTop_toTopOfid/iv_avatar android:layout_marginStart16dp app:layout_goneMarginStart0dp TextView android:idid/tv_name android:layout_widthmatch_parent android:layout_heightwrap_content android:text张三 android:textSize18sp android:textStylebold/ TextView android:idid/tv_title android:layout_widthmatch_parent android:layout_heightwrap_content android:text高级UI设计师 android:textSize14sp/ /LinearLayout !-- 认证标志 - 可能隐藏 -- ImageView android:idid/iv_verified android:layout_width24dp android:layout_height24dp app:layout_constraintEnd_toEndOfparent app:layout_constraintTop_toTopOfid/iv_avatar app:layout_goneMarginEnd0dp android:srcdrawable/ic_verified/ !-- 底部按钮组 -- LinearLayout android:layout_widthmatch_parent android:layout_heightwrap_content android:orientationhorizontal app:layout_constraintTop_toBottomOfid/iv_avatar app:layout_constraintStart_toStartOfparent android:layout_marginTop16dp Button android:layout_width0dp android:layout_heightwrap_content android:layout_weight1 android:text关注 android:layout_marginEnd8dp/ Button android:layout_width0dp android:layout_heightwrap_content android:layout_weight1 android:text私信/ /LinearLayout /androidx.constraintlayout.widget.ConstraintLayout这个案例展示了头像使用百分比宽度和1:1比例确保圆形用户信息区域使用goneMargin处理认证标志的显示/隐藏底部按钮组使用权重确保等宽分布整体布局在各种情况下都能保持协调的视觉效果

相关文章:

ConstraintLayout的‘隐藏技巧’:用百分比、比例和GoneMargin搞定复杂UI适配

ConstraintLayout高级适配技巧:百分比、比例与动态隐藏视图的完美解决方案 在Android开发中,ConstraintLayout已经成为构建复杂界面的首选布局方式。但许多开发者仅仅停留在基础使用层面,未能充分发挥其强大的适配能力。本文将深入探讨三个关…...

Taotoken官方折扣与Token套餐带来的成本优势感知

🚀 告别海外账号与网络限制!稳定直连全球优质大模型,限时半价接入中。 👉 点击领取海量免费额度 Taotoken官方折扣与Token套餐带来的成本优势感知 1. 成本感知的起点:计费透明 对于使用大模型API的开发者或团队而言&…...

QThread 最坑的不是启动,而是怎么把它停下来

QThread 真正麻烦的地方,不是 start 很多人第一次用 QThread,感觉还挺顺。创建线程,moveToThread,connect 几个信号,start 一下,任务跑起来,界面不卡了,心里还挺美。我以前也这么觉得…...

惠普tank 2606屏幕显示 er-08 ,加了粉还是报错er08,黄灯闪烁成像鼓接近寿命期限?亲测完美修复。

下载:点这里下载 备用下载:https://pan.baidu.com/s/1J7PN4m4fbIzku9DqBFg_nw?pwd0000...

树莓派4B + Python3 + OpenCV 实时扫码:从CSI摄像头调试到pyzbar优化,解决高延迟卡顿问题

树莓派4B动态扫码性能调优实战:从硬件选型到代码级优化 在智能仓储、自动化物流和创客项目中,实时二维码识别一直是核心需求。树莓派4B凭借其均衡的算力和丰富的扩展接口,成为这类场景的首选平台。但当开发者尝试用PythonOpenCVpyzbar构建动…...

网络安全自学顺序|千万不要搞反了

网络安全自学顺序|千万不要搞反了 想入行网络安全?别瞎学!这帮你少走半年弯路👇 从0到1进阶路径(按顺序学): 1.计算机网络基础(TCP/IP、OSI模型) 2.Linux系统与命令行…...

企业AI知识库搭建实战:从文件管理到智能检索的完整方案

2025年我们团队做过一个调研,找了37家用了AI知识库的企业,发现一个有意思的规律:真正用起来的不到1/3,剩下2/3基本都卡在同一个地方——知识库和文件管理系统是割裂的。 你让员工把文件再上传一遍到知识库?没人干。你让…...

G3000,MG3660,MG3640S,TS3380,G3800,TS3480,TS3680,TS3460,TS3350,MG6380报错5B00,P07,E08,1700,5b04废墨垫清零,好用

下载:点这里下载 备用下载:https://pan.baidu.com/s/1WrPFvdV8sq-qI3_NgO2EvA?pwd0000 常见型号如下: G系列 G1000、G1100、G1200、G1400、G1500、G1800、G1900、G1010、G1110、G1120、G1410、G1420、G1411、G1510、G1520、G1810、G1820、…...

通过curl命令快速测试TaotokenAPI兼容性与连通性教程

🚀 告别海外账号与网络限制!稳定直连全球优质大模型,限时半价接入中。 👉 点击领取海量免费额度 通过curl命令快速测试Taotoken API兼容性与连通性教程 在集成大模型服务时,开发者通常需要一种快速、轻量的方式来验证…...

神经网络概念解码:从物理直觉到工程权衡的思维地图

1. 项目概述:这不是又一本“手把手写反向传播”的书,而是一张神经网络的思维地图“NN#2 — Neural Networks Decoded: Concepts Over Code”这个标题里,“NN#2”不是版本号,而是刻意设计的编号——它暗示这是一场持续进行的认知迭…...

QQ音乐格式转换终极指南:如何3步将.qmc文件转为MP3/FLAC

QQ音乐格式转换终极指南:如何3步将.qmc文件转为MP3/FLAC 【免费下载链接】qmc-decoder Fastest & best convert qmc 2 mp3 | flac tools 项目地址: https://gitcode.com/gh_mirrors/qm/qmc-decoder 你是否曾在QQ音乐下载了心爱的歌曲,却发现它…...

移动端部署福音?YOLOv5结合EfficientNetV2主干网络的轻量化改造与性能实测

YOLOv5与EfficientNetV2融合:移动端目标检测的轻量化实践 在移动端和边缘计算设备上部署目标检测模型始终面临计算资源有限、功耗敏感等挑战。本文将深入探讨如何通过将YOLOv5与EfficientNetV2主干网络结合,构建一个真正适合嵌入式设备的轻量化目标检测…...

3步解决游戏手柄兼容性问题:XOutput完全指南

3步解决游戏手柄兼容性问题:XOutput完全指南 【免费下载链接】XOutput DirectInput to XInput wrapper 项目地址: https://gitcode.com/gh_mirrors/xo/XOutput 你是否遇到过这样的尴尬时刻?心爱的旧手柄在最新游戏里毫无反应,或者新买…...

DCIM存内计算技术:原理、挑战与自动化设计实践

1. 存内计算技术演进与DCIM核心挑战在AI计算架构的发展历程中,存内计算(Computing-in-Memory, CIM)技术正在引发一场深刻的范式变革。传统冯诺依曼架构中,数据需要在处理器和存储器之间频繁搬运,这种"内存墙"…...

告别串口助手:用Python脚本实现YMODEM协议自动升级嵌入式固件(附源码)

告别串口助手:用Python脚本实现YMODEM协议自动升级嵌入式固件(附源码) 在嵌入式设备量产测试和远程维护场景中,传统的手动串口工具操作已成为效率瓶颈。每次固件升级都需要人工介入,不仅耗时费力,还容易因…...

Auto数据集实战:用线性回归讲透建模全流程

1. 项目概述:为什么我坚持用Auto数据集讲透线性回归的“第一课” 你打开任何一本统计学习或机器学习入门书,几乎都会在第二章看到那个熟悉的表格——几列数字:mpg、cylinders、displacement、horsepower、weight、acceleration、model year、…...

PyTorch新手必看:RuntimeError: mat1 and mat2 shapes cannot be multiplied 的三种常见场景与快速排查法

PyTorch矩阵维度冲突实战指南:从报错原理到精准修复 当你满怀期待地按下运行键,等待模型开始训练时,突然跳出的RuntimeError: mat1 and mat2 shapes cannot be multiplied就像一盆冷水浇下来。这个在PyTorch中频繁出现的矩阵乘法维度错误&am…...

喜马拉雅音频下载神器:3步搞定VIP付费专辑的终极完整指南

喜马拉雅音频下载神器:3步搞定VIP付费专辑的终极完整指南 【免费下载链接】xmly-downloader-qt5 喜马拉雅FM专辑下载器. 支持VIP与付费专辑. 使用GoQt5编写(Not Qt Binding). 项目地址: https://gitcode.com/gh_mirrors/xm/xmly-downloader-qt5 想要轻松下载…...

3步快速定位:哪个程序偷走了你的Windows快捷键?

3步快速定位:哪个程序偷走了你的Windows快捷键? 【免费下载链接】hotkey-detective A small program for investigating stolen key combinations under Windows 7 and later. 项目地址: https://gitcode.com/gh_mirrors/ho/hotkey-detective 你是…...

还在为图表制作烦恼?Mermaid Live Editor让你3分钟搞定专业图表

还在为图表制作烦恼?Mermaid Live Editor让你3分钟搞定专业图表 【免费下载链接】mermaid-live-editor Edit, preview and share mermaid charts/diagrams. New implementation of the live editor. 项目地址: https://gitcode.com/GitHub_Trending/me/mermaid-li…...

如何在Windows上实现高效屏幕标注:gInk免费工具完全指南

如何在Windows上实现高效屏幕标注:gInk免费工具完全指南 【免费下载链接】gInk An easy to use on-screen annotation software inspired by Epic Pen. 项目地址: https://gitcode.com/gh_mirrors/gi/gInk 你是否需要在演示时快速圈出重点,或在线…...

WarcraftHelper终极教程:5分钟让魔兽争霸3焕发新生

WarcraftHelper终极教程:5分钟让魔兽争霸3焕发新生 【免费下载链接】WarcraftHelper Warcraft III Helper , support 1.20e, 1.24e, 1.26a, 1.27a, 1.27b 项目地址: https://gitcode.com/gh_mirrors/wa/WarcraftHelper 还在为《魔兽争霸3》在现代电脑上运行不…...

智能家居语音交互进阶:从离线识别到场景化意图推理的本地化实现

1. 项目概述:从“听见”到“听懂”的智能家居进化 “小爱同学,打开客厅灯。” “天猫精灵,空调调到26度。” 这类语音交互如今已司空见惯。但你是否遇到过这样的场景:对着音箱说“我有点冷”,它却回答“对不起&#xf…...

CANN 生态工具链:ATC、ACL 与 MindX 全景

一、CANN 工具链全景 1.1 工具链架构 ┌──────────────────────────────────────────────────┐ │ CANN 工具链全景 │ ├──────────────────────────────…...

CANN Profiling 与性能分析:定位训练与推理瓶颈

一、为什么需要 Profiling 1.1 性能问题的来源 深度学习训练和推理的性能瓶颈可能来自多个环节:数据准备慢导致 GPU 空闲、模型算子计算慢成为瓶颈、内存拷贝频繁拖累整体、通信带宽受限拖慢分布式训练。不同瓶颈的优化方法完全不同,错误的优化方向不仅浪…...

机器学习评价指标之基础指标与综合指标

基础指标评价指标的计算方法在两种方法中有一些差异,但它们都提供了对模型性能的有效度量,用于评估模型在多分类任务中的表现。具体选择哪种方法取决于任务需求、数据特点以及模型训练的策略。对于直接对多个类别进行预测的多分类模型,准确率…...

Fusion360新手必看:这10个隐藏快捷键和技巧,让你建模效率翻倍

Fusion360效率革命:10个被低估的实战技巧与深度应用 第一次打开Fusion360时,我被它复杂的界面吓到了——工具栏密密麻麻的图标,嵌套多层的右键菜单,还有那些隐藏在角落里的功能选项。直到一位资深用户向我演示了如何用长按左键快…...

碧蓝航线全皮肤解锁终极指南:Perseus补丁五分钟快速上手

碧蓝航线全皮肤解锁终极指南:Perseus补丁五分钟快速上手 【免费下载链接】Perseus Azur Lane scripts patcher. 项目地址: https://gitcode.com/gh_mirrors/pers/Perseus 还在为碧蓝航线中那些精美的舰娘皮肤需要付费解锁而烦恼吗?想要免费体验所…...

布局先行、技术深耕:国内端侧AI企业抢滩机器人与具身智能赛道

具身智能作为AI与物理世界交互的核心方向,正成为工业智能化、人形机器人落地的关键抓手。国内一批端侧AI企业凭借原生技术优势,早早入局机器人与具身智能领域,以全栈技术、规模化落地与生态共建,抢占行业先发优势。其中&#xff0…...

告别低效编程:在PyCharm 2024.1中配置Baidu Comate的保姆级教程(含快捷键设置)

告别低效编程:在PyCharm 2024.1中配置Baidu Comate的保姆级教程(含快捷键设置) 作为一名长期使用PyCharm进行Python开发的工程师,我深刻体会到重复性编码工作对创造力的消耗。直到遇见Baidu Comate——这款能与IDE深度集成的AI编码…...