zoukankan      html  css  js  c++  java
  • android getWidth()和getMeasuredWidth()方法的区别

    getWidth()

    Return the width of the your view.

    Returns

    • The width of your view, in pixels.

    源代码:

    public final int getWidth() {  
            return mRight - mLeft;  
        }

    getwidth返回的是右边坐标减轻坐标减去左边坐标,这要在布局之后才能确定它们的坐标,也就是说在布局后才能调用getwidth来获取。所以getWidth()获得的宽度是View在设定好布局后整个View的宽度。

    getMeasuredWidth()

    Return the full width measurement information for this view as computed by the most recent call to measure(int, int). This result is a bit mask as defined byMEASURED_SIZE_MASK and MEASURED_STATE_TOO_SMALL. This should be used during measurement and layout calculations only. Use getWidth() to see how wide a view is after layout.

    Returns

    The measured width of this view as a bit mask.

    得到的是最近一次调用measure()方法测量后得到的是View的宽度,它应该仅仅用在测量和Layout的计算中。再看源码:

    public final int getMeasuredWidth() {  
        return mMeasuredWidth & MEASURED_SIZE_MASK;  
    }  

    return The raw measured width of this view 获得的是原始的测量宽度。所以说getMeasuredWidth()是对View上的内容进行测量后得到的View内容占据的宽度。前提是你必须在父布局的onLayout()方法或者此View的onDraw()方法里调用measure(0,0);(measure中的参数的值你自己可以定义),否则你得到的结果和getWidth()得到的结果是一样的。

    区别

    getMeasuredWidth:对View上的内容进行测量后得到的View内容占据的宽度的。

    getWidth:View在设定号布局后整个View的宽度。

    使用场合

    getMeasuredWidth:在自定义view重写onLayout时、在我们用layoutinflater动态加载view后想获得view的原始宽度时。

    getWidth:一般在view已经布局后呈现出来了,想获取宽度时。


     
    Android <wbr>一张图理解getWidth和getMeasuredWidth
    view.getMeasuredHeight()可能隐藏在上头.可能在下头.如果view没有超出屏幕的时候view.getMeasuredHeight() 有可能小于 view.getHeight()
     
     getWidth(): View在設定好佈局後整個View的寬度。
      getMeasuredWidth(): 對View上的內容進行測量後得到的View內容佔據的寬度
  • 相关阅读:
    POJ 2456 Aggressive cows
    POJ 1064 Cable master
    POJ 3723 Conscription
    左偏树
    tarjan模板
    [bzoj5017][Snoi2017]炸弹 tarjan缩点+线段树优化建图+拓扑
    [BZOJ4520][Cqoi2016]K远点对 kd-tree 优先队列
    [bzoj3218]a + b Problem 网络流+主席树优化建图
    #6034. 「雅礼集训 2017 Day2」线段游戏 李超树
    【UOJ UNR #1】火车管理 可持久化线段树
  • 原文地址:https://www.cnblogs.com/zquan/p/9623912.html
Copyright © 2011-2022 走看看