Activity中获取view的高度和宽度为0的原因以及解决方案
2016-05-14 11:15
393 查看
原文链接:http://blog.csdn.net/nailsoul/article/details/25909313
在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上 如点击事件 触摸事件 焦点事件等
[java] view
plain copy
View view=findViewById(R.id.tv);
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int width = v.getWidth();
}
});
2.在activity被显示出来时即添加到了DecorView上时获取宽和高如onWindowFocusChanged() 回调方法
[java] view
plain copy
@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
[java] view
plain copy
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删除该回调
[java] view
plain copy
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为把界面渲染到窗口的时间
[java] view
plain copy
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.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上 如点击事件 触摸事件 焦点事件等
[java] view
plain copy
View view=findViewById(R.id.tv);
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int width = v.getWidth();
}
});
2.在activity被显示出来时即添加到了DecorView上时获取宽和高如onWindowFocusChanged() 回调方法
[java] view
plain copy
@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
[java] view
plain copy
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删除该回调
[java] view
plain copy
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为把界面渲染到窗口的时间
[java] view
plain copy
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);
}
相关文章推荐
- HDU 1198 Farm Irrigation (并查集)
- 结构体的学习
- shell(二)Bash常用快捷键
- hdu 3746 Cyclic Nacklace(kmp)
- Spring MVC静态资源处理<mvc:resources><mvc:default-servlet-handler>
- SDAU 练习三 1007 天上掉馅饼问题
- 各分辨率图片生成神器
- 软考二进宫-总结知识点2
- hdu 1213 How Many Tables
- ElasticSearch 攻略(一)
- 2016“百度之星”-测试赛
- getBackground().setAlpha所导致问题
- Beaver's Calculator 【归并排序+贪心】
- 手机开发实战17——CSD与GPRS的比较
- ArrayList源码分析
- 数据库中间件的收集
- 如何自学Android
- jQuery基于扩展实现的倒计时效果
- Flume快速入门(一):背景简介
- Mainfest.xml文件的初步解析