zoukankan      html  css  js  c++  java
  • android获取view宽高的几种方法

    在onCreate方法中我们通过mView.getWidth()和mView.getHeight()获取到的view的宽高都是0,那么下面几种方法就可以在onCreate方法中获取到view的宽高。

    1、

     int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
            int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
            mTextView.measure(w, h);
            int height = mTextView.getMeasuredHeight();
            int width = mTextView.getMeasuredWidth();
            System.out.println("measure width=" + width + " height=" + height);

    2、mViewTreeObserver = mTextView.getViewTreeObserver();

    mViewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener()
            {
                @Override
                public void onGlobalLayout()
                {
                    // TODO Auto-generated method stub
                    mTextView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                    System.out.println("onGlobalLayout width=" + mTextView.getWidth() + " height=" + mTextView.getHeight());
                }
            });

    3、

    mViewTreeObserver.addOnPreDrawListener(new OnPreDrawListener()
            {
                @Override
                public boolean onPreDraw()
                {
                    // TODO Auto-generated method stub
                    mTextView.getViewTreeObserver().removeOnPreDrawListener(this);
                    System.out.println("onPreDraw width=" + mTextView.getWidth() + " height=" + mTextView.getHeight());
                    return true;
                }
            });

    4、

    @Override
        public void onWindowFocusChanged(boolean hasFocus)
        {
            // TODO Auto-generated method stub
            super.onWindowFocusChanged(hasFocus);
            if (hasFocus)
            {
                System.out.println("onWindowFocusChanged width=" + mTextView.getWidth() + " height=" + mTextView.getHeight());
            }
        }

    打印结果:

    本文转自:http://www.cnblogs.com/yushilong/p/3924741.html?utm_source=tuicool

  • 相关阅读:
    Oracle EBS 更改物料说明后,在MTL_SYSTEM_ITEMS_B表中无变化
    Oracle EBS 复制用户职责
    Oracle EBS 多节点停应用
    Oracle EBS AR 更新客户
    Oracle EBS AP更新供应商地址
    Oracle EBS AP 供应商地点失效
    Oracle AP更新供应商
    string 从下标0 一直截到倒数第三位
    一个不错的spring 学习博客
    Filter及FilterChain的使用详解
  • 原文地址:https://www.cnblogs.com/sishuiliuyun/p/4882354.html
Copyright © 2011-2022 走看看