正确获取View的宽高
来源:互联网 发布:excel2013破解软件 编辑:程序博客网 时间:2024/06/06 06:52
获取View宽和高的方法:
(在onCreate、onStart、onResume中均无法正确得到某个View的宽/高信息,这是因为View的measure过程和Activity的生命周期方法不是同步执行的,因此无法保证Activity执行了OnCreate、onStart、onResume时某个View已经测量完毕了,如果View还没有测量完毕,那么获得的宽/高就是0)
提供四种解决方案:
1、Activity/View#onWindowFocusChanged
onWindowFocusChanged这个方法的含义是:View已经初始化完毕了,宽/高已经准备好了,这个时候去获取宽/高是没有问题的。需要注意的是,onWindowFocusChanged会被调用多次,当Activity的窗口得到焦点和是去焦点时均会被调用一次。具体来说,当Activity继续执行或暂停执行时,onWindowFocusChanged均会被调用,如果频繁的进行onResume、omPause,那么onWindowFocusChanged也会被频繁的调用。具体代码如下:
public void onWindowFocusChanged(boolean hasFocus){ super.onWindowFocusChanged(hasFocus); if(hasFocus){ int width=view.getMeasuredWidth(); int height=view.getMeasuredHeight(); }}
2、View.post(runnable)
通过post可以将一个runnable投递到消息队列的尾部,然后等待Looper调用此runnable的时候,View也已经初始化好了。具体代码如下:
protected void onStart(){ super.onStart(); view.post(new Runnable(){ @Override public void run(){ int width=view.getMeasuredWidth(); int height=view.getMeasuredHeight(); } });}
3、ViewTreeObserver
使用ViewTreeObserver的众多回调可以完成这个功能,比如使用OnGlobalLayoutListener这个接口,当View树的状态发生改变或者View树内部的View的可见性发生改变时,onGlobalLayout方法即将被回调,因此这是获取View的宽/高一个很好的时机。但需要注意的是,伴随着View树的状态改变等,onGlobalLayout会被调用多次。代码如下:
protected void start(){ super.onStart(); ViewTreeObserver observer=view.getViewTreeObserver(); observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener(){ @SuppressWarnings("deprecation") @Override public void onGlobalLayout(){ view.getViewTreeObserver(). removeGlobalOnLayoutListener(this); int width=view.getMeasuredWidth(); int height=view.getMeasuredHeight(); } });}
4、view.measure(int widthMeasureSpec,int heightMeasureSpec)
通过手动对View进行measure来得到View的宽/高。但是这种情况比较复杂,需要分情况处理。根据View的LayoutParams来分:
match_parent
直接放弃,无法measure出具体的宽/高。根据View的measure过程,构造MeasureSpec需要知道parentSize,即父容器的剩余空间,而这个时候我们无法知道parentSize的大小,所以理论上不可能测量出View的大小。
具体的数值(dp/px)
比如宽高都是100px,如下measure:
int widthMeasureSpec=MeasureSpec.makeMeasureSpec(100, MeasureSpec.EXACTLY);int heightMeasureSpec=MeasureSpec.makeMeasureSpec(100, MeasureSpec.EXACTLY);view.measure(widthMeasureSpec,heightMeasureSpec);
warp_content:
int widthMeasureSpec=MeasureSpec.makeMeasureSpec((1<<30)-1, MeasureSpec.AT_MOST); int heightMeasureSpec=MeasureSpec.makeMeasureSpec((1<<30)-1, MeasureSpec.AT_MOST);view.measure(widthMeasureSpec,heightMeasureSpec);
注意到(1<<30 )-1,通过分析MeasureSpec的实现可以知道,View的尺寸使用30位二进制表示, 也就是说最大是30个1(即2^30-1),在最大化模式下,我们用View理论上能支持的最大值去构造MeasureSpec是合理的。
两种错误的使用方法:
1、
int widthMeasureSpec=MeasureSpec.makeMeasureSpec(-1,MeasureSpec.UNSPECIFIED);int heightMeasureSpec=MeasureSpec.makeMeasureSpec(-1,MeasureSpec.UNSPECIFIED);view.measure(widthMeasureSpec,heightMeasureSpec);
2、
view.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
错误的原因:首先其违背了系统的内部实现规范(因为无法通过错误的MeasureSpec去得出合法的SpecMode,从而导致measure过程出错),其次不能保证一定能measure出正确的结果。
- 正确获取View的宽高
- 在onCreate中正确获取View的宽高
- 在activity中如何正确获取View的宽高
- Android 获取 View 宽高的常用正确方式,避免为零
- Android 获取 View 宽高的常用正确方式,避免为零
- Android 获取 View 宽高的常用正确方式,避免为零
- Android 获取 View 宽高的常用正确方式,避免为零
- Android 获取 View 宽高的常用正确方式,避免为零
- 获取View的宽高
- 获取view宽高
- 获取view宽高
- View 的宽高的获取
- view的宽高获取不到
- 获取view的高和宽
- Android获取View的高宽
- Android 如何获取view的宽高
- 浅谈自定义View的宽高获取
- 获取View的宽高信息
- Linux下搭建nginx(centOS 64位系统)
- 冒泡排序和快排的交换次数
- # !/usr/bin/python 和 #!/usr/bin/env python 区别
- Fiori学习笔记
- HashMap源码分析
- 正确获取View的宽高
- jetty 配置数据源以及应用
- 解决Vue打包后背景图片路径错误问题
- c语言掩码结构体
- dubbo配置
- hibernate之order by子句
- C实现DNS协议(MacOS & Linux)
- JS基础学习第十三天:Math对象方法和Date日期方法
- mxGraph