android view getWidth 和 getHeight 的值为0
程序员文章站
2022-07-14 17:45:42
...
The getWidth and getHeight methods will return 0 if the view has not yet been inflated. For example, if you are trying to access it in the onCreate of the Activity, you'll get zero.
在UI 组件还未显示在界面之前调用getWidth和getHeight方法通常会得到0。所以不能在onCreate方法里获得组件的width和height.
可以通过以下两种方法获得:
float width=activity.getWindowManager().getDefaultDisplay().getWidth();
float height=activity.getWindowManager().getDefaultDisplay().getHeight();
或者重写onWindowFocusChanged,在这个方法里获取
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
view.getHeight();
view.getWidth();
}
转载于:https://my.oschina.net/MrGuan/blog/61314
推荐阅读
-
android之view获取getWidth()和getHeight()
-
android view getWidth 和 getHeight 的值为0
-
Android之View的知识(getWidth() 和getMeasuredWidth区别 如何在oncreate获取宽高)
-
控件getHeight和getWidth等于0的解决办法
-
视图的getWidth()和getHeight()返回0
-
Android调用getWidth()或getHeight()获取视图尺寸为0的解决办法
-
Android 获得view的宽和高,为0
-
Android自定义View5--getWidth()和getMeasuredWidth()区别, view.post(Runnable)引发内存泄漏的原因和解决
-
第四十二题 UVA1152 和为0的4个值 4 Values whose Sum is 0
-
在storyboard中view的frame值为0