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();}


更多相关文章

  1. android 获取web 内容简单实现
  2. Android通过url获取bitmap图片问题
  3. android通过NFC读取卡号
  4. android去除标题栏-------全屏运行AND 无标题栏 总结
  5. Android(安卓)源码解析 - ScrollView
  6. 系统命令行运行 android app程序
  7. Android(安卓): 自己写个HTTP框架
  8. [置顶] Android(安卓)系统应用调用,intent 的使用方法总结
  9. android遇到的BUG

随机推荐

  1. 总结mysql 常用DDL, DML语言并实操;
  2. 缓存函数的简单使用
  3. Windows还原系统后分区合并为一个分区恢
  4. php简单的数组递归
  5. 通过链式调用,将一个类委托给另一个类实现
  6. 今天记录下极光推送
  7. 几种常用设计模式的简单示例
  8. 命名空间自动加载进阶与mysql初体验
  9. phpquery中文手册
  10. php递归将数组拼接为url