Android -- 获取View宽高
在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。
View的事件回调里使用
这时候该view已经被显示即被添加到DecorView上 如点击事件 触摸事件 焦点事件等
View view=findViewById(R.id.tv);
view.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
int width = v.getWidth();
}
});
onWindowFocusChanged() 回调
在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();
Log.i("onWindowFocusChanged() "+msg1);
Log.i("onWindowFocusChanged() "+msg2);
super.onWindowFocusChanged(hasFocus);
}
延时
在onResume方法最后开线程300毫秒左右后获取宽和高 因为onResume执行完后300毫秒后,界面就显示出来了。
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();
Log.i("onWindowFocusChanged() "+msg1);
Log.i("onWindowFocusChanged() "+msg2);
}
}, 300);
在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();
Log.i("onWindowFocusChanged() "+msg1);
Log.i("onWindowFocusChanged() "+msg2);
}
}, 300);
}
});
我是天王盖地虎的分割线
参考:http://blog.csdn.net/nailsoul/article/details/25909313
Android -- 获取View宽高的更多相关文章
- view--4种Android获取View宽高的方式
有时我们会有基于这样的需求,当Activity创建时,需要获取某个View的宽高,然后进行相应的操作,但是我们在onCreate,onStart中获取View的大小,获取到的值都是0,只是由于View ...
- android获取view宽高的几种方法
在onCreate方法中我们通过mView.getWidth()和mView.getHeight()获取到的view的宽高都是0,那么下面几种方法就可以在onCreate方法中获取到view的宽高. ...
- android获取屏幕宽高与获取控件宽高
获取屏幕宽高 // 获取屏幕宽高(方法1) int screenWidth = getWindowManager().getDefaultDisplay().getWidth(); // 屏幕宽(像素 ...
- Activity正确获取View宽高
在View的measure完成后,一般可以通过getMeasureWidth/getMeasureWidth方法可以正确的获取View的宽高,而在特殊情况下,可能需要多次measure才能确定最终的测 ...
- android 获取屏幕宽高 和 获取控件坐标
一.获取屏幕宽高: (1). WindowManager wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE); int width ...
- 获取view宽高
在oncreate()中利用view.getWidth()或是view.getHeiht()来获取view的宽和高,看似没有问题,其实他们去得值是0,并不是你想要的结果? 这是为什么呢? 在调用onc ...
- android onCreate中获取view宽高为0的解决方法
view.post(runnable) 通过post可以将一个runnable投递到消息队列的尾部,然后等待UI线程Looper调用此runnable的时候,view也已经初始化好了. view.po ...
- android中加载的html获取的宽高不正确
wap页面使用 js库是zepto,按照惯例在$(function(){})中,来获取当前可视区的宽高,但得到的宽高却与预想的相差十万八千里. 原本android手机的浏览器设定的宽高基本是360*6 ...
- Android笔记之获取显示器宽高
原先的Display.getWidth().Display.getHeight()已废弃 推荐的获取Display宽高的方法如下 DisplayMetrics metrics = new Displa ...
随机推荐
- hdu 5752 Sqrt Bo 水题
Sqrt Bo 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5752 Description Let's define the function f ...
- Linux学习笔记05—文件与目录权限
1. 绝对路径与相对路径绝对路径:路径的写法一定由根目录 ‘/’写起,例如 /usr/local/mysql 这就是绝对路径相对路径:路径的写法不是由根目录 ‘/’写起,例如:首先用户进入到/, 然后 ...
- 设置Azure WebSite黑白名单
Azure WebSite服务默认是不提供黑白名单,也就是说任何Internet用户都可以访问Azure WebSite,那么我们如何来给我们的网站设置黑白名单? 这里有一种方式,可以通过配置网站的配 ...
- HDU 3974 Assign the task(简单线段树)
Assign the task Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- oracle 两个逗号分割的字符串 如何判断是否其中有相同值
比如字段A: 'ab,cd,ef,gh'字段B: 'aa,bb,cc,dd' 没有相同值 字段A: 'ab,cd,ef,gh'字段B: 'aa,bb,cd,dd' 有相同值cd 1.CREATE OR ...
- Jquery DataTable基本使用
1,首先需要引用下面两个文件 <link rel="stylesheet" href="https://cdn.datatables.net/1.10.16/css ...
- VC设置代理方法
参考文章: VC 设置代理 Setting and Retrieving Internet Options Change Internet Proxy settings http://suppor ...
- DTrace memory leak 内存泄露
http://blog.sina.com.cn/s/blog_538040b70100eecn.html 如下程序用于跟踪,在分配和回收都会触发探针 #!/usr/sbin/dtrace -s p ...
- TF400511: Your team has not defined any iterations to use as sprints
tfs里面的冲刺对于开发团队来说, 是非常重要的一个功能,是团队开发进度的晴雨表: 但是如果从此死活出不来,怎么办呢? TF400511:您的团队尚未定义任何要用作冲刺 (sprint) 的迭代 TF ...
- Android 应用开发特色
Android 系统到底提供了哪些东西,供我们可以开发出优秀的应用程序.1. 四大组件Android 系统四大组件分别是活动(Activity).服务(Service).广播接收器(Broadcast ...