Android(安卓)在Activity中获取控件尺寸的方法

上Layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <TextView        android:id="@+id/tv_hello"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world" />LinearLayout>

上Activity,几种方法都写下来了:

package com.cn.measure;import android.annotation.SuppressLint;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.ViewTreeObserver;import android.view.ViewTreeObserver.OnGlobalLayoutListener;import android.widget.TextView;import android.widget.Toast;@SuppressLint("NewApi")public class MainActivity extends Activity{    private TextView tv_hello;    @Override    protected void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        tv_hello = (TextView)findViewById(R.id.tv_hello);        getSizeWithViewTreeObserver();        getSizeWithPost();        getSizeWithMeasureByHand();    }    /**     * 如果tv_hello的mode是match_parent不能用这种方法     * 因为view的measure过程中需要知道父容器的剩余空间大小,这个时候无法知道父容器剩余空间大小     * 其实这种方法不推荐使用,测量出来的值可能不正确     */    private void getSizeWithMeasureByHand()    {        tv_hello.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);        int height = tv_hello.getMeasuredHeight();        int width = tv_hello.getMeasuredWidth();        Toast.makeText(MainActivity.this,            "getSizeWithMeasureByHand excuted: width is " + width + " \n Height is " + height,            0).show();    }    private void getSizeWithPost()    {        tv_hello.post(new Runnable()        {            @Override            public void run()            {                int width = tv_hello.getMeasuredWidth();                int height = tv_hello.getMeasuredHeight();                Toast.makeText(MainActivity.this,                    "getSizeWithPost excuted: width is " + width + " \n Height is " + height,                    0).show();            }        });    }    private void getSizeWithViewTreeObserver()    {        ViewTreeObserver observer = tv_hello.getViewTreeObserver();        observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener()        {            @Override            public void onGlobalLayout()            {                tv_hello.getViewTreeObserver().removeGlobalOnLayoutListener(this);                int width = tv_hello.getMeasuredWidth();                int height = tv_hello.getMeasuredHeight();                Toast.makeText(MainActivity.this,                    "getSizeWithViewTreeObserver excuted: width is " + width + " \n Height is " + height,                    0).show();            }        });    }    @Override    public void onWindowFocusChanged(boolean hasFocus)    {        super.onWindowFocusChanged(hasFocus);        int width = tv_hello.getMeasuredWidth();        int height = tv_hello.getMeasuredHeight();        Toast.makeText(MainActivity.this,            "onWindowFocusChanged excuted: width is " + width + " \n Height is " + height,            0).show();    }}

四种方法:

  private void getSizeWithMeasureByHand() //不推荐使用  private void getSizeWithViewTreeObserver() //推荐使用  private void getSizeWithPost() //推荐使用  public void onWindowFocusChanged(boolean hasFocus) //推荐使用