Android-获取view的宽高

本文介绍了在Android开发中获取View宽高的五种方法:通过监听点击事件、使用addOnGlobalLayoutListener、利用view.post或view.postDelayed、监听窗口焦点变化以及在Activity的生命周期方法中获取。通过示例代码详细展示了各种方法的应用场景及效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

方式:

方式1:监听,如setOnClickListener

方式2:监听,如addOnGlobalLayoutListener

方式3:view.post 或 view.postdelay

方式4:onWindowFocusChanged

方式5:

示例:

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="获取view的宽高" />
</LinearLayout>

代码:

public class MyActivity extends FragmentActivity {

    private Button btn;
    private int mHeight;
    private int mMeasuredHeight;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test3);

        btn = ((Button) this.findViewById(R.id.btn));

        mHeight = btn.getHeight();
        mMeasuredHeight = btn.getMeasuredHeight();
        Log.e("111", "onCreate==" + mHeight + " | " + mMeasuredHeight);//onCreate==0 | 0

        //方式1:监听,如setOnClickListener
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mHeight = btn.getHeight();
                mMeasuredHeight = btn.getMeasuredHeight();
                Log.e("111", "onClick==" + mHeight + " | " + mMeasuredHeight);//onClick==144 | 144
            }
        });

        //方式2:监听,如addOnGlobalLayoutListener
        btn.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onGlobalLayout() {
                btn.getViewTreeObserver().removeOnGlobalLayoutListener(this);

                mHeight = btn.getHeight();
                mMeasuredHeight = btn.getMeasuredHeight();
                Log.e("111", "onGlobalLayout==" + mHeight + " | " + mMeasuredHeight);//onGlobalLayout==144 | 144
            }
        });

        //方式3:view.post
        btn.post(new Runnable() {
            @Override
            public void run() {
                mHeight = btn.getHeight();
                mMeasuredHeight = btn.getMeasuredHeight();
                Log.e("111", "post==" + mHeight + " | " + mMeasuredHeight);//post==144 | 144
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();

        mHeight = btn.getHeight();
        mMeasuredHeight = btn.getMeasuredHeight();
        Log.e("111", "onResume==" + mHeight + " | " + mMeasuredHeight);//onResume==0 | 0
    }

    @Override
    protected void onStart() {
        super.onStart();
        mHeight = btn.getHeight();
        mMeasuredHeight = btn.getMeasuredHeight();
        Log.e("111", "onStart==" + mHeight + " | " + mMeasuredHeight);//onStart==0 | 0
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);

        mHeight = btn.getHeight();
        mMeasuredHeight = btn.getMeasuredHeight();
        Log.e("111", "onWindowFocusChanged==" + mHeight + " | " + mMeasuredHeight);//onWindowFocusChanged==144 | 144
    }
}

打印log顺序:

onCreate==0 | 0
onStart==0 | 0
onResume==0 | 0
onGlobalLayout==144 | 144
post==144 | 144
onWindowFocusChanged==144 | 144
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值