温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android 获得View宽高的几种方式总结

发布时间:2020-10-06 19:53:13 来源:脚本之家 阅读:127 作者:_小马快跑_ 栏目:移动开发

《Android开发艺术探索》笔记:

在Activity的onCreate()或者onResume()中去获得View的高度的时候不能正确获得宽度和高度信息,这是因为 View的measure过程和Activity的生命周期不是同步执行的,因此无法保证Activity执行了onCreate onStart onResume时,某个View已经测量完毕了,如果还没有测量完,那么获得的宽高就是0。可以通过下面几种方式来获得:

1、onWindowFocusChanged

onWindowFocusChanged:View已经初始化完毕,宽高已经有了,需要注意onWindowFocusChanged会被调用多次,Activity得到焦点和失去焦点都会执行这个回调,见下图:

Android 获得View宽高的几种方式总结

1、Activity首次进入的时候执行的方法

2、跳转到另一个Activity时

3、返回到当前Activity时
可见当执行onResume和onPause时,onWindowFocusChanged都会被调用。

 @Override
 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也已经初始化好了,示例:

 @Override
 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会被调用多次,示例:

@Override
protected void onStart() {
  super.onStart();
  ViewTreeObserver observer=view.getViewTreeObserver();
  observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
      view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
      int width=view.getMeasuredWidth();
      int height=view.getMeasuredHeight();
    }
  });
}

4、view.measure(int widthMeasureSpec, int heightMeasureSpec)

通过手动对View进行measure来得到View的宽高,这里要分情况处理,根据View的LayoutParams来分:

match-parent

无法测出具体的宽高,因为根据View的measure过程,构造此种MeasureSpec需要知道parentSize,即父容器的剩余空间,而这个值我们是不知道的,所以不能测出View的大小。

具体的数值(dp/px)

比如宽高都是100px,如下measure:

 int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(100, View.MeasureSpec.EXACTLY);
 int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(100, View.MeasureSpec.EXACTLY);
 view.measure(widthMeasureSpec, heightMeasureSpec);

wrap_content

如下measure:

 int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec((1 << 30) - 1, View.MeasureSpec.AT_MOST);
 int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec((1 << 30) - 1, View.MeasureSpec.AT_MOST);
 view.measure(widthMeasureSpec, heightMeasureSpec);

View的specSize使用30位二进制表示,也就是说最大是30个1,也就是(1 << 30) - 1,在最大化模式下,我们用View理论上能支持的最大值去构造MeasureSpec是合理的。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI