美文网首页
Android fragment中view的width为0

Android fragment中view的width为0

作者: yunhen | 来源:发表于2019-08-27 11:25 被阅读0次

fragment 中没有 onWindowFocusChanged () ,所以要给一个根Layout设置视图树观察者

ViewTreeObserver vto = layRootView.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(rootGlobalLayoutListener);



public ViewTreeObserver.OnGlobalLayoutListener rootGlobalLayoutListener
            =  new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
          int  imgWidth = view.getWidth();
            LogUtils.e(TAG,"OnGlobalLayoutListener imgWidth=  " + imgWidth);
           
            // 成功调用一次后,移除方法,防止被反复调用
            layRootView.getViewTreeObserver().removeOnGlobalLayoutListener(rootGlobalLayoutListener);
        }
    };

相关文章

网友评论

      本文标题:Android fragment中view的width为0

      本文链接:https://www.haomeiwen.com/subject/dfnsectx.html