1.等布局完成後再進行布局
fun isSafeToRequestDirectly():Boolean {
return if (isInLayout) {
// when isInLayout == true and isLayoutRequested == true,
// means that this layout pass will layout current view which will
// make currentView.isLayoutRequested == false, and this will let currentView
// ignored in process handling requests called during last layout pass.
isLayoutRequested.not()
} else {
var ancestorLayoutRequested = false
var p: ViewParent? = parent
while (p != null) {
if (p.isLayoutRequested) {
ancestorLayoutRequested = true
break
}
p = p.parent
}
ancestorLayoutRequested.not()
}
}
fun safeRequestLayout() {
if (isSafeToRequestDirectly()) {
requestLayout()
} else {
post { requestLayout() }
}
}
2.請求根布局
getWindow().getDecorView().requestLayout();