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

Android 开发问题:View 的 getWidth、getHeight 方法返回的值都为 0

ImageViewivTest=findViewById(R.id.iv_test);intwidth=ivTest.getWidth();intheight=ivTest.getHeight();Log.i(TAG,"width: "+width);Log.i(TAG,"height: "+height);
# 输出结果 width: 0 height: 0
  • 在 Android 开发中,上述代码中,getWidth、getHeight 方法返回的值都为 0
问题原因
  1. 当一个 Activity 启动时,视图的创建和显示需要经过几个关键阶段:视图创建阶段 -> 测量阶段 -> 布局阶段 -> 绘制阶段

  2. 在 onCreate 或过早的阶段调用这些方法时,视图尚未完成测量和布局流程,因此坐标值尚未计算,默认返回 0

处理策略
  • 使用View.post方法,确保视图已完成布局
ImageViewivTest=findViewById(R.id.iv_test);ivTest.post(()->{intwidth=ivTest.getWidth();intheight=ivTest.getHeight();Log.i(TAG,"width: "+width);Log.i(TAG,"height: "+height);});
补充学习
  • 在 onCreate 方法中,​​任何需要等待测量和布局阶段完成后才能确定的信息​​都无法获取,例如
  1. 尺寸相关方法​​:getWidth、getHeight、getMeasuredWidth、getMeasuredHeight
ImageViewivTest=findViewById(R.id.iv_test);intwidth=ivTest.getWidth();intheight=ivTest.getHeight();intmeasuredWidth=ivTest.getMeasuredWidth();intmeasuredHeight=ivTest.getMeasuredHeight();Log.i(TAG,"width: "+width);Log.i(TAG,"height: "+height);Log.i(TAG,"measuredWidth: "+measuredWidth);Log.i(TAG,"measuredHeight: "+measuredHeight);
# 输出结果 width: 0 height: 0 measuredWidth: 0 measuredHeight: 0
// 使用 View.post 方法处理后ImageViewivTest=findViewById(R.id.iv_test);ivTest.post(()->{intwidth=ivTest.getWidth();intheight=ivTest.getHeight();intmeasuredWidth=ivTest.getMeasuredWidth();intmeasuredHeight=ivTest.getMeasuredHeight();Log.i(TAG,"width: "+width);Log.i(TAG,"height: "+height);Log.i(TAG,"measuredWidth: "+measuredWidth);Log.i(TAG,"measuredHeight: "+measuredHeight);});
# 输出结果 width: 200 height: 200 measuredWidth: 200 measuredHeight: 200
  1. 相对位置相关方法​​:getLeft、getRight、getTop、getBottom、getX、getY
ImageViewivTest=findViewById(R.id.iv_test);intleft=ivTest.getLeft();intright=ivTest.getRight();inttop=ivTest.getTop();intbottom=ivTest.getBottom();floatx=ivTest.getX();floaty=ivTest.getY();Log.i(TAG,"left: "+left);Log.i(TAG,"right: "+right);Log.i(TAG,"top: "+top);Log.i(TAG,"bottom: "+bottom);Log.i(TAG,"x: "+x);Log.i(TAG,"y: "+y);
# 输出结果 left: 0 right: 0 top: 0 bottom: 0 x: 0.0 y: 0.0
// 使用 View.post 方法处理后ImageViewivTest=findViewById(R.id.iv_test);ivTest.post(()->{intleft=ivTest.getLeft();intright=ivTest.getRight();inttop=ivTest.getTop();intbottom=ivTest.getBottom();floatx=ivTest.getX();floaty=ivTest.getY();Log.i(TAG,"left: "+left);Log.i(TAG,"right: "+right);Log.i(TAG,"top: "+top);Log.i(TAG,"bottom: "+bottom);Log.i(TAG,"x: "+x);Log.i(TAG,"y: "+y);});
# 输出结果 left: 0 right: 200 top: 161 bottom: 361 x: 0.0 y: 161.0
  1. 绝对位置​​相关方法:getLocationOnScreen、getLocationInWindow、getGlobalVisibleRect
ImageViewivTest=findViewById(R.id.iv_test);int[]locationOnScreen=newint[2];ivTest.getLocationOnScreen(locationOnScreen);int[]locationInWindow=newint[2];ivTest.getLocationInWindow(locationInWindow);Rectrect=newRect();ivTest.getGlobalVisibleRect(rect);Log.i(TAG,"locationOnScreen: "+Arrays.toString(locationOnScreen));Log.i(TAG,"locationInWindow: "+Arrays.toString(locationInWindow));Log.i(TAG,"rect: "+rect);
# 输出结果 locationOnScreen: [0, 0] locationInWindow: [0, 0] rect: Rect(0, 0 - 0, 0)
// 使用 View.post 方法处理后ImageViewivTest=findViewById(R.id.iv_test);ivTest.post(()->{int[]locationOnScreen=newint[2];ivTest.getLocationOnScreen(locationOnScreen);int[]locationInWindow=newint[2];ivTest.getLocationInWindow(locationInWindow);Rectrect=newRect();ivTest.getGlobalVisibleRect(rect);Log.i(TAG,"locationOnScreen: "+Arrays.toString(locationOnScreen));Log.i(TAG,"locationInWindow: "+Arrays.toString(locationInWindow));Log.i(TAG,"rect: "+rect);});
# 输出结果 locationOnScreen: [0, 161] locationInWindow: [0, 161] rect: Rect(0, 161 - 200, 361)
http://www.gsyq.cn/news/1506096.html

相关文章:

  • 国内专业陶艺技能技法培训机构实力排行盘点 - 起跑123
  • PCA9532 I2C LED驱动芯片:从原理到实践的完整指南
  • 黑神话悟空实时地图导航插件:告别迷路的终极指南
  • 百度网盘真实下载地址解析终极指南:告别龟速下载的完整解决方案
  • Firefox隐私强化配置包:禁用SafeBrowsing+防指纹+JS权限收紧的user.js一键部署方案
  • 【小白向】 OpenClaw 配置教程,附带运行故障全套解决办法(包含安装包)
  • FANUC驱动板维修用高清原理图包:含电源电路、IPM驱动与编码器接口实拍图及参数说明
  • SAP财务与销售数据打通实战:用VF04增强自动填充凭证文本和合同号(附完整Z表创建指南)
  • AI在科研中的角色演进:从工具到协作伙伴
  • MS140132KT SH-POTS芯片组:模拟电话接入数字网络的完整解决方案
  • 权威认证!2026年6月浪琴全国官方维修地址汇总,官方售后服务电话持续可用 - 信息热点
  • 兆易创新推出全新光模块专用MCU,聚力光互联产业升级
  • 如何用5分钟将单张插画变成专业PSD分层文件:Layerdivider终极指南
  • 远程农业大棚监控系统(双核心架构 + 预训练模型和云端大模型 + LCD显示 + 无线通信)
  • 【Kafka源码解读和使用指南】第44篇:Kafka日志存储源码解析(三)——OffsetIndex稀疏索引的秘密武器
  • 售价 80 美元!罗技超便携鼠标 Mobi Fold 发布,小巧功能多但需适应
  • [STM32]Day11-软件实现SPI读写W25Q64
  • 12305华夏之光永存:黄大年茶思屋榜文123期 第5题多图层图像生成(鸿蒙5.0)工程落地终版
  • 微信小程序会议管理源码:支持发布会议、嵌入直播、查看参会记录
  • 用XUnity.AutoTranslator轻松突破语言障碍:Unity游戏翻译完整指南
  • KMS_VL_ALL_AIO:企业级Windows与Office智能激活解决方案技术深度解析
  • MSC7116 DSP硬件设计实战:时钟、复位与电源序列的避坑指南
  • DLOS AI OS v1.0:面向大语言模型输出的双环控制操作系统
  • 终极指南:3步掌握RePKG工具的高级资源提取与转换技巧
  • 重塑办公界面:Office Custom UI Editor的界面定制革命
  • 2026企业团建策划避坑指南:云南5大优质服务商深度盘点 - 品研笔录
  • 如何快速实现微博图片批量下载:终极免登录指南
  • CKS 2024实战指南:16个核心安全场景深度解析
  • SolidWorks服务器+云飞云共享云桌面 = 10人共享方案
  • KTV、剧场、政企场馆,不同场景舞台灯光厂家该怎么挑 - 深度智识库