標籤:
廢話只重複兩句;
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的原理