当前位置: 首页 > 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.jsqmd.com/news/994333/

相关文章:

  • PCA9532 I2C LED驱动芯片:从原理到实践的完整指南
  • MC9S12XHY GPIO寄存器深度解析:从基础配置到中断与复用实战
  • 2026年宁波留学机构十强榜单:十家精选品牌深度盘点 - 信息热点
  • 黑神话悟空实时地图导航插件:告别迷路的终极指南
  • DLOS AI操作系统:基于双环验证架构的AI输出治理系统
  • 如何快速上手北理工BIThesis论文模板:终极完整指南
  • 百度网盘真实下载地址解析终极指南:告别龟速下载的完整解决方案
  • Firefox隐私强化配置包:禁用SafeBrowsing+防指纹+JS权限收紧的user.js一键部署方案
  • 2026 硅胶热转印标定制厂家盘点 口碑工厂技术产品全解析 - 变量人生001
  • 向量空间JBoltAI:企业大脑与数字员工的底层逻辑
  • 【小白向】 OpenClaw 配置教程,附带运行故障全套解决办法(包含安装包)
  • 437天,陈航二次执掌钉钉成败几何?92年技术极客陈宇森接棒续写新篇
  • FANUC驱动板维修用高清原理图包:含电源电路、IPM驱动与编码器接口实拍图及参数说明
  • 手把手教你用CH32V307的GPIO模拟3线SPI点亮HX8347屏(附完整源码)
  • 华为战略预备队,解决什么问题?
  • SAP财务与销售数据打通实战:用VF04增强自动填充凭证文本和合同号(附完整Z表创建指南)
  • Matlab电力负荷预测代码包:TCN-LSTM-Attention混合模型+黑猩猩算法自动调参
  • MC9S12HZ256 DBGV1硬件调试模块:从原理到实战的嵌入式开发利器
  • 人生要快速失败的具象化的庖丁解牛
  • 高端制造行业新一代信息技术EDA 工业软件行业技术岗软件开发工程师晋升CTO都要经历哪些职位?
  • 199 元诺基亚 200 4G 新机登场,微聊功能能否打破功能机局限?
  • 2026 数码喷绘吸墨涂层行业主流厂商与技术应用深度解读 - 变量人生001
  • 软件定义无线电芯片OL2385:工业物联网无线节点的智能射频收发方案
  • 撕开AI落地实战的‘遮羞布’:为何传统培训总是纸上谈兵?这3大暗坑必须规避
  • AI在科研中的角色演进:从工具到协作伙伴
  • MS140132KT SH-POTS芯片组:模拟电话接入数字网络的完整解决方案
  • 12304华夏之光永存:黄大年茶思屋榜文123期 第4题大语言模型快慢思考模式混合训练(工程落地终版)
  • 权威认证!2026年6月浪琴全国官方维修地址汇总,官方售后服务电话持续可用 - 信息热点
  • 兆易创新推出全新光模块专用MCU,聚力光互联产业升级
  • Arduino平台DFPlayer Mini MP3模块即插即用驱动包(含串口兼容方案与多场景示例)