android获取view宽高的几种方法
在onCreate方法中我们通过mView.getWidth()和mView.getHeight()获取到的view的宽高都是0,那么下面几种方法就可以在onCreate方法中获取到view的宽高。
1、
int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
mTextView.measure(w, h);
int height = mTextView.getMeasuredHeight();
int width = mTextView.getMeasuredWidth();
System.out.println("measure width=" + width + " height=" + height);
2、mViewTreeObserver = mTextView.getViewTreeObserver();
mViewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener()
{
@Override
public void onGlobalLayout()
{
// TODO Auto-generated method stub
mTextView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
System.out.println("onGlobalLayout width=" + mTextView.getWidth() + " height=" + mTextView.getHeight());
}
});
3、
mViewTreeObserver.addOnPreDrawListener(new OnPreDrawListener()
{
@Override
public boolean onPreDraw()
{
// TODO Auto-generated method stub
mTextView.getViewTreeObserver().removeOnPreDrawListener(this);
System.out.println("onPreDraw width=" + mTextView.getWidth() + " height=" + mTextView.getHeight());
return true;
}
});
4、
@Override
public void onWindowFocusChanged(boolean hasFocus)
{
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
if (hasFocus)
{
System.out.println("onWindowFocusChanged width=" + mTextView.getWidth() + " height=" + mTextView.getHeight());
}
}
打印结果:

本文转自:http://www.cnblogs.com/yushilong/p/3924741.html?utm_source=tuicool
android获取view宽高的几种方法的更多相关文章
- view--4种Android获取View宽高的方式
有时我们会有基于这样的需求,当Activity创建时,需要获取某个View的宽高,然后进行相应的操作,但是我们在onCreate,onStart中获取View的大小,获取到的值都是0,只是由于View ...
- Android -- 获取View宽高
在activity中可以调用View.getWidth.View.getHeight().View.getMeasuredWidth() .View.getgetMeasuredHeight()来获得 ...
- Activity启动过程中获取组件宽高的五种方式
第一种:(重写Activity的onWindowFocusChanged方法) /** * 重写Acitivty的onWindowFocusChanged方法 */ @Override public ...
- Android获取APK包名的几种方法
Android获取APK包名的几种方法:1.adb shell pm list package -f | findstr 关键字 #只能获取到包名,主Activity名无法获取到 2.使用aapt-- ...
- Activity正确获取View宽高
在View的measure完成后,一般可以通过getMeasureWidth/getMeasureWidth方法可以正确的获取View的宽高,而在特殊情况下,可能需要多次measure才能确定最终的测 ...
- android获取屏幕宽高与获取控件宽高
获取屏幕宽高 // 获取屏幕宽高(方法1) int screenWidth = getWindowManager().getDefaultDisplay().getWidth(); // 屏幕宽(像素 ...
- android 获取屏幕宽高 和 获取控件坐标
一.获取屏幕宽高: (1). WindowManager wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE); int width ...
- 获取view宽高
在oncreate()中利用view.getWidth()或是view.getHeiht()来获取view的宽和高,看似没有问题,其实他们去得值是0,并不是你想要的结果? 这是为什么呢? 在调用onc ...
- JS获取元素宽高的两种情况
JS获取元素宽高分两种情况, 一.内联样式,也就是直接把width和height写在HTML元素中的style里: 这种情况使用 document.getElementById('xxx'). ...
随机推荐
- 洛咕 P4199 万径人踪灭
给了两条限制,但是第二条想想是没用的,直接manacher就可以减掉多余的部分了,所以要求满足第一条的方案 也不难,可以想到枚举每个中心点,计算两边有多少对距离中心相等的位置值也相等,假设有\(t\) ...
- Kali2.0可用国内源更新sources.list
vim /etc/apt/sources.list #中科大 deb http://mirrors.ustc.edu.cn/kali kali-rolling main non-free contri ...
- c# table 怎么在前台循环展示 ViewBag
后台 public ActionResult DoctorEvaluation()//前台页面 { HE_Department HE_dt = new HE_Department(); DataTab ...
- python+appium 实现qq聊天的消息,滑动删除聊天消息
有人问我,appium怎么去删除qq聊天的, 当时想到的是滑动, 可是具体的大概有个思路,于是乎,就想自己来实现下, 打开模拟器,开发者选项,找到显示坐标的 然后去打开qq获取要删除的消息的坐标后, ...
- C++模板的实现(模板函数和模板类,附带模板实现顺序表和链表代码)
文章链接:https://blog.csdn.net/qq_38646470/article/details/80209469
- 角色和武器Shader特效开发
角色Shader的动效需求 角Shader的开发不知要实现最基础光照等功能, 可能还要在角色武器的Shader增加多种动效, 比如因武器品质区分的流光特效, 被技能击中时的冻结效果. 这类动效的实现方 ...
- 教你如何编写、保存与运行 Python 程序
第一步 接下来我们将看见如何在 Python 中运行一个传统的“Hello World”程序.Python教程本章将会教你如何编写.保存与运行 Python 程序. 通过 Python 来运行的你的程 ...
- could not launch process: decoding dwarf section info at offset 0x0: too short
Fabric调试异常 作者在使用chaincode进行智能合约开发的过程中,使用Goland + Golang + win10_X64作为开发环境: GoLand 2018.1.4 Build #GO ...
- IPC_Binder_java_2
title: IPC_Binder_java_2 date: 2017-07-04 14:47:55 tags: [IPC,Binder] categories: [Mobile,Android] - ...
- Python序列之列表 (list)
作者博文地址:http://www.cnblogs.com/spiritman/ 列表是Python中最基本的数据结构,是Python最常用的数据类型.Python列表是任意对象的有序集合,通过索引访 ...