美文网首页
android获取控件宽高

android获取控件宽高

作者: 约定_6976 | 来源:发表于2018-09-26 09:59 被阅读0次

    1,

    tvtest.post(new Runnable() {

    @Override

        public void run() {

    Toast.makeText(MainActivity.this, tvtest.getWidth() +"--" +tvtest.getHeight(), Toast.LENGTH_SHORT).show();

        }

    });

    2,

    ViewTreeObservervto2 = ll_sticky_header.getViewTreeObserver();

    vto2.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

    @Override

    public void onGlobalLayout() {

    heightHeader = ll_sticky_header.getMeasuredHeight();

    }});

    View.post()中的run方法和onWindowFocusChanged方法几乎是同时执行,都是在onLayout之后,所以都是可以得到控件的宽高的!!!

    3,

    @Override

    public void onWindowFocusChanged(boolean hasFocus) {

        super.onWindowFocusChanged(hasFocus);

        mWidth = mTextView.getWidth();

        mHeight = mTextView.getHeight();

        Log.d(TAG, "onWindowFocusChanged: width = " + mWidth + "  height = " + mHeight);

    }

    oncreate()→onResume()→onMeasure()→onLayout()→onWidnowFocusChanged()→.....→onDraw()...

    4,自定义view

    onMeasure()完成才能通过getMeasuredHeight()和getMeasuredWidth()获取宽高

    onLayout()完成才能通过getHeight()和getWidth()获取宽高

    相关文章

      网友评论

          本文标题:android获取控件宽高

          本文链接:https://www.haomeiwen.com/subject/bhqdoftx.html