有时候我们须要在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. MariaDB数据库(二)

    1. MariaDB数据类型 MariaDB数据类型可以分为数字,日期和时间以及字符串值. 使用数据类型的原则:够用就行,尽量使用范围小的,而不用大的. 1.1  常用的数据类型 整数:int,bit ...

  2. rails使用bootstrap

    在Gemfile文件中添加'bootstrap-sass',再运行bundle install gem 'bootstrap-sass' 在config/application.rb添加一行代码,让b ...

  3. 宝塔apache配置

    apache配置 <VirtualHost *:80> ServerAdmin webmaster@example.com DocumentRoot "/www/wwwroot/ ...

  4. 学习JSON

    JSON:JavaScript  Object  Notation,轻量级的文本数据交换格式 .是存储和交换文本信息的语法.类似XML.但  JSON比XML更小.更快,更易解析.JSON使用Java ...

  5. 大数据学习——yum安装tomcat

    https://www.cnblogs.com/jtlgb/p/5726161.html 安装tomcat6 yum install tomcat6 tomcat6-webapps tomcat6-a ...

  6. 大数据学习——Linux上常用软件安装

    4.1 Linux系统软件安装方式 Linux上的软件安装有以下几种常见方式: 1.二进制发布包 软件已经针对具体平台编译打包发布,只要解压,修改配置即可 2.RPM发布包 软件已经按照redhat的 ...

  7. 【Ts 1】 maven初识

    PS:本篇博客,就是对于maven的一个简单的总结,认识.可能更多的是借鉴别人的看法,然后结合自己的使用,再加以说明. 首先,什么是maven: Apache Maven is a software ...

  8. python 快排,堆排,归并

    #归并排序 def mergeSort(a,L,R) :     if(L>=R) :         return     mid=((L+R)>>1)     mergeSort ...

  9. HDU 2767 Proving Equivalences(强连通 Tarjan+缩点)

    Consider the following exercise, found in a generic linear algebra textbook. Let A be an n × n matri ...

  10. python之-- socket 基础篇

    socket 网络模块 注意事项:在python3中,所有数据的传输必须用bytes类型(bytes只支持ascii码)所以在发送数据的时候要么在发送的字符串前面加 'b',要么使用encode('u ...