android中View的GONE和INVISIBLE的原理

來源:互聯網
上載者:User

標籤:

廢話只重複兩句;

GONE真的隱藏;

INVISIBLE不可見但是預留了View的位置;

網上千篇一律的重複著這兩句話並舉著例子,並沒有觀察本質來作區分。查看源碼後得知其區別希望廣大朋友能夠借鑒,源碼如下:

 /* Check if the GONE bit has changed */        if ((changed & GONE) != 0) {            needGlobalAttributesUpdate(false);            requestLayout();            if (((mViewFlags & VISIBILITY_MASK) == GONE)) {                if (hasFocus()) clearFocus();                clearAccessibilityFocus();                destroyDrawingCache();                if (mParent instanceof View) {                    // GONE views noop invalidation, so invalidate the parent                    ((View) mParent).invalidate(true);                }                // Mark the view drawn to ensure that it gets invalidated properly the next                // time it is visible and gets invalidated                mPrivateFlags |= PFLAG_DRAWN;            }            if (mAttachInfo != null) {                mAttachInfo.mViewVisibilityChanged = true;            }        }        /* Check if the VISIBLE bit has changed */        if ((changed & INVISIBLE) != 0) {            needGlobalAttributesUpdate(false);            /*             * If this view is becoming invisible, set the DRAWN flag so that             * the next invalidate() will not be skipped.             */            mPrivateFlags |= PFLAG_DRAWN;            if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE)) {                // root view becoming invisible shouldn‘t clear focus and accessibility focus                if (getRootView() != this) {                    if (hasFocus()) clearFocus();                    clearAccessibilityFocus();                }            }            if (mAttachInfo != null) {                mAttachInfo.mViewVisibilityChanged = true;            }        }

 

如果在GONE和INVISIBLE兩者都可以完成你的效果,那麼你應該選擇INVISIBLE。因為從源碼中來看GONE需要重新的布局和通知上級View去重新整理,有緩衝還要清空緩衝;從視圖變更開銷的來說INVISIBLE要更加的划算一些,如果你的View不是十分佔用資源的情況!!!也非常歡迎大家說出自己的看法。

android中View的GONE和INVISIBLE的原理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.