Android进阶笔记18:Android 获取Button的高度、宽度、坐标值

时间:2021-02-24 03:36:27

1. 问题

如何获取一个控件的长和高,相信很多朋友第一眼看见这个问题都会觉得很简单,直接在onCreate里面调用getWidth、getMeasuredWidth不就可以获得了吗,但是,事实上是并没有简单的,不信的话,你可以去试一下,在onCreate里面,你是无法获得长宽值的,始终为0

 package com.himi.test;

 import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.Button; public class MainActivity extends Activity { private Button button; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.button);
Log.e("Test", "getWidth宽度:" + button.getWidth());
Log.e("Test", "getHeight高度:" + button.getMeasuredWidth());
} }

程序运行结果如下:

Android进阶笔记18:Android 获取Button的高度、宽度、坐标值

2. 原因

这是为什么呢,其实熟悉view绘制流程的朋友应该一眼就看出来了,在onCreate中,我们的控件其实还并没有画好,换句话说,等onCreate方法执行完了,我们定义的控件才会被度量(measure),所以我们在onCreate方法里面通过view.getHeight()获取控件的高度或者宽度肯定是0。

3. 获取Button的高度、宽度、坐标值:

(1)利用延时(保证onCreate先完成):

 package com.himi.test;

 import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.Button; public class MainActivity extends Activity { private Button button; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.button);
Log.e("Test", "getWidth宽度:" + button.getWidth());
Log.e("Test", "getHeight高度:" + button.getMeasuredWidth()); new Thread() { @Override
public void run() {
synchronized (this) {
try {
wait(1000);// 1秒
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} Log.e("Test", "高度:" + button.getHeight());
Log.e("Test", "宽度:" + button.getWidth()); Log.e("Test", "左上角坐标x:" + button.getLeft());
Log.e("Test", "左上角坐标y:" + button.getTop()); Log.e("Test", "右下角坐标x:" + button.getRight());
Log.e("Test", "右下角坐标y:" + button.getBottom()); }
}.start(); } }

运行程序如下:

Android进阶笔记18:Android 获取Button的高度、宽度、坐标值

(2)自己使用API测量:

 package com.himi.test;

 import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button; public class MainActivity extends Activity { private Button button; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.button); int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
button.measure(w, h); int height = button.getMeasuredHeight();
int width = button.getMeasuredWidth(); Log.e("Test", "宽度:" + width);
Log.e("Test", "高度:" + height); Log.e("Test", "左上角坐标x:" + button.getLeft());
Log.e("Test", "左上角坐标x:" + button.getTop()); Log.e("Test", "左上角坐标x:" + button.getRight());
Log.e("Test", "左上角坐标x:" + button.getBottom()); } }

运行程序如下:
Android进阶笔记18:Android 获取Button的高度、宽度、坐标值

(3)我们需要注册一个ViewTreeObserver的监听回调,这个监听回调OnGlobalLayoutListener,就是全局的布局改变监听器,所以是最推荐使用的:

ViewTreeObserver:这是一个注册监听视图树的观察者(observer),在视图树种全局事件改变时得到通知。这个全局事件不仅还包括整个树的布局,从绘画过程开始,触摸模式的改变等。ViewTreeObserver不能够被应用程序实例化,因为它是由视图提供,参照getViewTreeObserver()以查看更多信息。

 package com.himi.test;

 import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.Button; public class MainActivity extends Activity { private Button button; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.button); final ViewTreeObserver vto = button.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
button.getViewTreeObserver().removeGlobalOnLayoutListener(this);
int height = button.getMeasuredHeight();
int width = button.getMeasuredWidth(); Log.e("Test", "高度:" + height);
Log.e("Test", "宽度:" + width); Log.e("Test", "左上角坐标x:" + button.getLeft());
Log.e("Test", "左上角坐标y:" + button.getTop()); Log.e("Test", "右下角坐标x:" + button.getRight());
Log.e("Test", "右下角坐标y:" + button.getBottom()); }
});
} }

部署程序到手机上,如下:

Android进阶笔记18:Android 获取Button的高度、宽度、坐标值