有时候我们须要在Activity的时候获取控件的宽和高来做一些操作,以下介绍三种获取宽和高的方式:

1. onWindowFocusChanged

@Override
public void onWindowFocusChanged(boolean hasFocus)
{
super.onWindowFocusChanged(hasFocus);
if (hasFocus)
{
int width = image.getMeasuredWidth();
int height = image.getMeasuredHeight();
Toast.makeText(MainActivity.this, "width = " + width + "---height = " + height, Toast.LENGTH_SHORT).show();
} }

2. post方式

 @Override
protected void onStart()
{
super.onStart(); image.post(new Runnable()
{ @Override
public void run()
{
int width = image.getMeasuredWidth();
int height = image.getMeasuredHeight();
Toast.makeText(MainActivity.this, "onstart--width = " + width + "---height = " + height, Toast.LENGTH_SHORT).show();
}
});
}

3. ViewTreeObserver

 ViewTreeObserver observer = image.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener()
{ @Override
public void onGlobalLayout()
{
image.getViewTreeObserver().removeGlobalOnLayoutListener(this);
int width = image.getMeasuredWidth();
int height = image.getMeasuredHeight();
Toast.makeText(MainActivity.this, "onglobal--width = " + width + "---height = " + height, Toast.LENGTH_SHORT).show();
}
});

备注:以上image是ImageView控件

ImageView image = (ImageView)findViewById(R.id.image);

【Android】获取控件的宽和高的更多相关文章

  1. Android初级教程_获取Android控件的宽和高

    转载:http://blog.csdn.net/johnny901114/article/details/7839512 我们都知道在onCreate()里面获取控件的高度是0,这是为什么呢?我们来看 ...

  2. 我的Android进阶之旅------>如何获取Android控件的宽和高

    本文转载于:http://blog.csdn.net/johnny901114/article/details/7839512 我们都知道在onCreate()里面获取控件的高度是0,这是为什么呢?我 ...

  3. 【转】获取Android控件的宽和高

    我们都知道在onCreate()里面获取控件的高度是0,这是为什么呢?我们来看一下示例: 首先我们自己写一个控件,这个控件非常简单: public class MyImageView extends ...

  4. [BS-01] 根据字符串对象的参数自动计算用来显示该字符串的UI控件的宽和高

    根据字符串对象的参数自动计算用来显示该字符串的UI控件的宽和高 1.  影响昵称Label的高和宽的因素: 字体和字号大小.文字多少.高度取决于是否固定了宽度(是否限制了最大的宽度和高度) 2. 使用 ...

  5. Android在onCreate中获取控件的宽高

    在某些需求下,我们需要在onCreate的时候就获取到控件的宽高,但是如果直接用view.getWidth()或view.getHeight()会得到0.这是因为在onCreate执行的时候,控件还没 ...

  6. Android 获取控件滑动速度,速度跟踪器VelocityTracker;

    VelocityTracker 速度跟踪器 在写关于Android滑动的控件,如果用户手指在屏幕上(当前位置 - 起始位置 > 某个数值)就做一个界面切换,但是总感觉太生硬,只有满足上面的条件才 ...

  7. Android 获取控件相对于屏幕位置

    // View宽,高 public int[] getLocation(View v) { int[] loc = new int[4]; int[] location = new int[2]; v ...

  8. activity 中获取控件的宽高

    1.第一种方式: TextView textview3 = findViewById(R.id.textview3); textView3.post(new Runnable() { @Overrid ...

  9. android获取屏幕宽高与获取控件宽高

    获取屏幕宽高 // 获取屏幕宽高(方法1) int screenWidth = getWindowManager().getDefaultDisplay().getWidth(); // 屏幕宽(像素 ...

随机推荐

  1. 利用system-config-kickstart实现半自动化安装

    老司机开车了… 上车请坐稳… centos7系统 首先确认已经安装了system-config-kickstart包,如果没有安装就yum install system-config-kickstar ...

  2. CSS3---圆角设置

    1.border-radius是向元素添加圆角边框.border-radius:10px; /* 所有角都使用半径为10px的圆角 */     border-radius: 5px 4px 3px ...

  3. 第二天,学习if,变量,注释

    zz_age = 31guss_age=int(input("input your answer:"))if guss_age == zz_age: print ("Yo ...

  4. Python 文件操作(一)

    一.注意事项 A.能调用方法的一定是对象 B.文件的操作流程: 1. 打开文件,得到文件句柄并赋值给一个变量 2. 通过句柄对文件进行操作 3. 关闭文件 二.操作实现方法 '''文件名:小双双文件内 ...

  5. java多线程之ForkJoinPool

    转https://www.cnblogs.com/lixuwu/p/7979480.html 阅读目录 使用 背景:ForkJoinPool的优势在于,可以充分利用多cpu,多核cpu的优势,把一个任 ...

  6. 《算法导论》— Chapter 15 动态规划

    序 算法导论一书的第四部分-高级设计和分析技术从本章开始讨论,主要分析高效算法的三种重要技术:动态规划.贪心算法以及平摊分析三种. 首先,本章讨论动态规划,它是通过组合子问题的解而解决整个问题的,通常 ...

  7. 对于easyui $.messager.alert和 $.messager.confirm 不同浏览器中位置控制

    $.messager.confirm({ title: '种子购买', msg: '确定购买这个'+seedName+'的种子嘛?', top:, fn: function (r) { if (r){ ...

  8. STM32F407 串口通信实验 视频第27节 个人笔记

    前言 第26节也是串口,笔记链接在此:https://www.cnblogs.com/YuQiao0303/p/10019362.html github地址:https://github.com/Yu ...

  9. python011 Python3 字典

    Python3 字典字典是另一种可变容器模型,且可存储任意类型对象.字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中 ,格式如 ...

  10. vue移动端头像上传,不大于50K

    先看效果: 稍加说明一下:第一张图是user.vue,第二张图是点击头像出现的系统自带上传文件格式(安卓和IOS不一样),第三张图是cropper组件(我单独设置的),第四张图是上传完成的user.v ...