android view getWidth 和 getHeight 的值为0
16lz
2021-01-24
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();}
更多相关文章
- android 获取web 内容简单实现
- Android通过url获取bitmap图片问题
- android通过NFC读取卡号
- android去除标题栏-------全屏运行AND 无标题栏 总结
- Android(安卓)源码解析 - ScrollView
- 系统命令行运行 android app程序
- Android(安卓): 自己写个HTTP框架
- [置顶] Android(安卓)系统应用调用,intent 的使用方法总结
- android遇到的BUG