Activity中获取view的高度和宽度为0的原因以及解决方案
在activity中可以调用View.getWidth、View.getHeight()、View.getMeasuredWidth() 、View.getgetMeasuredHeight()来获得某个view的宽度或高度,但是在onCreate()、onStrart()、onResume()方法中会返回0,这是应为当前activity所代表的界面还没显示出来没有添加到WindowPhone的DecorView上或要获取的view没有被添加到DecorView上或者该View的visibility属性为gone 或者该view的width或height真的为0 所以只有上述条件都不成立时才能得到非0的width和height
所以要想获取到的width和height为真实有效的 则有以下方法
1.在该View的事件回调里使用 这时候该view已经被显示即被添加到DecorView上 如点击事件 触摸事件 焦点事件等
View view=findViewById(R.id.tv);
view.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
int width = v.getWidth();
}
});
2.在activity被显示出来时即添加到了DecorView上时获取宽和高如onWindowFocusChanged() 回调方法
@Override
public void onWindowFocusChanged(boolean hasFocus) {
View iv1 = findViewById(R.id.iv1);
View iv2=findViewById(R.id.iv2);
String msg1="iv1' width:"+iv1.getWidth()+" height:"+iv1.getHeight()+" measuredWidth:"+iv1.getMeasuredWidth()+"measuredHeight:"+iv1.getMeasuredHeight();
String msg2="iv2' width:"+iv2.getWidth()+" height:"+iv2.getHeight()+" measuredWidth:"+iv2.getMeasuredWidth()+"measuredHeight:"+iv2.getMeasuredHeight();
i("onWindowFocusChanged() "+msg1);
i("onWindowFocusChanged() "+msg2);
super.onWindowFocusChanged(hasFocus);
}
3.或在onResume方法最后开线程300毫秒左右后获取宽和高 因为onResume执行完后300毫秒后 界面就显示出来了
当然地2种和地3种方法要保证获取宽高的view是在setContentView时设进去的View或它的子View
view.postDelayed(new Runnable() {
@Override
public void run() {
View iv1 = findViewById(R.id.iv1);
View iv2=findViewById(R.id.iv2);
String msg1="iv1' width:"+iv1.getWidth()+" height:"+iv1.getHeight()+" measuredWidth:"+iv1.getMeasuredWidth()+"measuredHeight:"+iv1.getMeasuredHeight();
String msg2="iv2' width:"+iv2.getWidth()+" height:"+iv2.getHeight()+" measuredWidth:"+iv2.getMeasuredWidth()+"measuredHeight:"+iv2.getMeasuredHeight();
i("onWindowFocusChanged() "+msg1);
i("onWindowFocusChanged() "+msg2);
}
}, 300);
4.在onCreate()或onResume()等方法中需要获取宽高时使用getViewTreeObserver().addOnGlobalLayoutListener()来添为view加回调在回调里获得宽度或者高度获取完后让view删除该回调
view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
view.postDelayed(new Runnable() {
@Override
public void run() {
View iv1 = findViewById(R.id.iv1);
View iv2=findViewById(R.id.iv2);
String msg1="iv1' width:"+iv1.getWidth()+" height:"+iv1.getHeight()+" measuredWidth:"+iv1.getMeasuredWidth()+"measuredHeight:"+iv1.getMeasuredHeight();
String msg2="iv2' width:"+iv2.getWidth()+" height:"+iv2.getHeight()+" measuredWidth:"+iv2.getMeasuredWidth()+"measuredHeight:"+iv2.getMeasuredHeight();
i("onWindowFocusChanged() "+msg1);
i("onWindowFocusChanged() "+msg2);
}
}, 300);
}
});
5.在窗口焦点发生变化时获取宽高 onResume完后就会把界面渲染到窗口上 渲染完后将执行窗口焦点花生变化回调 所以onResume到 onWindowFocusChanged为把界面渲染到窗口的时间
boolean measure;
View iv1;
View iv2;
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if(hasFocus && !measure){
measure=true;
measure();
}
}
private void findViews(){
iv1 = findViewById(R.id.iv1);
iv2 =findViewById(R.id.iv2);
}
private void measure(){
String msg1="iv1' width:"+iv1.getWidth()+" height:"+iv1.getHeight()+" measuredWidth:"+iv1.getMeasuredWidth()+"measuredHeight:"+iv1.getMeasuredHeight();
String msg2="iv2' width:"+iv2.getWidth()+" height:"+iv2.getHeight()+" measuredWidth:"+iv2.getMeasuredWidth()+"measuredHeight:"+iv2.getMeasuredHeight();
i("onWindowFocusChanged() "+msg1);
i("onWindowFocusChanged() "+msg2);
}
Activity中获取view的高度和宽度为0的原因以及解决方案的更多相关文章
- 如何在onCreate中获取View的高度和宽度
如何在onCreate中获取View的高度和宽度 原文链接:http://mp.weixin.qq.com/s?__biz=MzAwODE1NTI2MQ==&mid=2247483676&am ...
- Android 在OnCreate()中获取控件高度与宽度
试过在OnCreate()中获取控件高度与宽度的童鞋都知道,getWidth()与getHeight()方法返回是0,具体原因 看一下Activity的生命周期 就会明白. 上代码: 方法一: int ...
- javascript中获取dom元素高度和宽度
javascript中获取dom元素高度和宽度的方法如下: 网页可见区域宽: document.body.clientWidth网页可见区域高: document.body.clientHeight网 ...
- web开发中获取的各种高度和宽度
前端开发中经常需要获取页面还有屏幕的高度和宽度进行计算,此文即介绍如何用 JavaScript 还有 jQuery 获取这些尺寸. 1.简介 一个页面显示在浏览器内,浏览器又放置在屏幕窗口内,所以由里 ...
- 解决在onCreate()过程中获取View的width和Height为0的4种方法
很经常当我们动态创建某些View时,需要通过获取他们的width和height来确定别的view的布局,但是在onCreate()获取view的width和height会得到0.view.getWid ...
- 解决在onCreate()过程中获取View的width和Height为0的方法
最近在看Android底层代码的view绘制原理的时候讲到一个很有意思的事情,也是我几年前刚开始学习Android开发的时候比较纳闷的一个问题,如果你不理解Android的底层绘制,请看我之前一片文章 ...
- Android 开发 Activity里获取View的宽度和高度 转载
原文地址:https://blog.csdn.net/chenbaige/article/details/77991594 前言: 可能很多情况下,我们都会有在activity中获取view 的尺寸大 ...
- android 获取屏幕的高度和宽度、获取控件在屏幕中的位置、获取屏幕中控件的高度和宽度
(一)获取屏幕的高度和宽度 有两种方法: 方法1: WindowManager wm = (WindowManager) getContext().getSystemService(Context.W ...
- Android查缺补漏(View篇)--在 Activity 的 onCreate() 方法中为什么获取 View 的宽和高为0?
在 Activity 的 onCreate() 方法中为什么获取 View 的宽和高为0 ? @Override protected void onCreate(Bundle savedInstanc ...
随机推荐
- DFX 安全测试-- 告诉你什么是XSS、sql注入?POST和GET的区别....
1.用户权限测试 (1) 用户权限控制 1) 用户权限控制主要是对一些有权限控制的功能进行验证 2) 用户A才能进行的操作,B是否能够进行操作(可通过窜session,将在下面介绍) 3)只能有A条件 ...
- Java Se: Logging 框架说明
Java Logging 用惯了log4j等日志工具,竟然不知Java还自带了个log工具.今天有空了就来了解一下. 先来看一个简单的例子: public class SystemTest { pri ...
- 使用Hive或Impala执行SQL语句,对存储在Elasticsearch中的数据操作
http://www.cnblogs.com/wgp13x/p/4934521.html 内容一样,样式好的版本. 使用Hive或Impala执行SQL语句,对存储在Elasticsearch中的数据 ...
- java 生产者消费者问题 并发问题的解决
引言 生产者和消费者问题是线程模型中的经典问题:生产者和消费者在同一时间段内共用同一个存储空间,如下图所示,生产者向空间里存放数据,而消费者取用数据,如果不加以协调可能会出现以下情况: 生产者消费者图 ...
- hadoop2.6---windows下开发环境搭建
一.准备插件 1.自己编译 1.1 安装Ant 官网下载Ant,apache-ant-1.9.6-bin.zip 配置环境变量,新建ANT_HOME,值是E:\apache-ant-1.9.6:PAT ...
- Mysql数据库笔记
出错记录: 1.mysql服务启动不了,进程意外终止 1067 错误显示:can not connect to mysql server on local hosts(1061) 解决方法 ...
- js生成[n,m]的随机数
一.预备知识 Math.ceil(); //向上取整. Math.floor(); //向下取整. Math.round(); //四舍五入. Math.random(); //0.0 ~ 1 ...
- [转]NopCommerce How to code my own payment method
本文转自:http://docs.nopcommerce.com/display/nc/How+to+code+my+own+payment+method Payment methods are im ...
- RSA Study
These days I study the RSA Algorithm. It is a little complex, but not very. Also, my study has not f ...
- Sass关于颜色函数的乐趣
阅读目录 1. 了解RGB和HSL颜色标准 2. RGB函数 3. HSL函数 4. Opacity函数 5. 其他颜色函数 6. 一个简单的应用 在Sass中,定义了很多现成的函数,可供我们使用.在 ...