第五周 關(guān)于View的知識(shí)

理解:關(guān)于View的知識(shí)

1. View的getWidth()和getMeasuredWidth()有什么區(qū)別嗎?

getWidth是獲取控件真實(shí)的寬度,在view.onlayout之后才能獲取到。
getMeasuredWidth()是獲取的計(jì)算寬度,在view.onmeasure之后獲取。
會(huì)受到measure影響,而結(jié)果不同.

    @ViewDebug.ExportedProperty(category = "layout")
    public final int getWidth() {
        return mRight - mLeft;
    }
    public final int getMeasuredWidth() {
        return mMeasuredWidth & MEASURED_SIZE_MASK;
    }

2.如何在OnCreate中拿到View的寬度和高度?

  1. view.post(runnable) 使用 主線程的handle發(fā)送message

能保證獲取的原因是因?yàn)椋?br> oncreate的時(shí)候mAttachInfo還為空,那些Runnable并沒有馬上被執(zhí)行,而是保存到RunQueue里面。
執(zhí)行的接口就是RunQueue.executeActions,其內(nèi)部也看到是調(diào)用Handler執(zhí)行的,RunQueue.executeActions()這個(gè)接口在整個(gè)ViewRootImpl里只有一個(gè)地方調(diào)用,就是在performTraversals(),但是由于是發(fā)送的延時(shí)消息而且executeActions其實(shí)是在onlayut之前調(diào)用,不太好說(屏幕是黑屏?xí)r,OnCreate獲取的還是0)

    public boolean post(Runnable action) {
        final AttachInfo attachInfo = mAttachInfo;
        if (attachInfo != null) {
            return attachInfo.mHandler.post(action);
        }

        // Postpone the runnable until we know on which thread it needs to run.
        // Assume that the runnable will be successfully placed after attach.
        getRunQueue().post(action);
        return true;
    }
  1. ViewTreeObserver.addOnPreDrawListener / ViewTreeObserver.addOnGlobalLayoutListener

在draw之前獲取監(jiān)聽(屏幕是黑屏?xí)r,OnCreate獲取的還是0)
ViewTreeObserver.addOnGlobalLayoutListener
可見性發(fā)送變化時(shí)(這個(gè)更適合)

        final ViewTreeObserver viewTreeObserver = textView.getViewTreeObserver();
        viewTreeObserver.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                Log.e("TAG","onPreDraw:"+textView.getWidth());
                textView.getViewTreeObserver().removeOnPreDrawListener(this);
                return false;
            }
        });

        viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Log.e("TAG","onGlobalLayout:"+textView.getWidth());
                textView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });
  1. textView.addOnLayoutChangeListener

在view的layout的時(shí)候

            if (li != null && li.mOnLayoutChangeListeners != null) {
                ArrayList<OnLayoutChangeListener> listenersCopy =
                        (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
                int numListeners = listenersCopy.size();
                for (int i = 0; i < numListeners; ++i) {
                    listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
                }
            }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容