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) //推荐使用
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值