在LinearLayout的测量方法measureHorizontal里,有个地方存在疑问,widthMode和isExactly赋初值之后,在后面的for循环整个流程里,其值并没有变化,按这个逻辑,如果下面第一份if语句能进去,第二个if则应该一直为true,else语句是永远不会执行的,但为何要这么写呢?
void measureHorizontal(int widthMeasureSpec, int heightMeasureSpec) {
final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
…
final boolean isExactly = widthMode == MeasureSpec.EXACTLY;
…
for (int i = 0; i < count; ++i) {
…
if (widthMode == MeasureSpec.EXACTLY && lp.width == 0 && lp.weight > 0) {
// Optimization: don't bother measuring children who are going to use
// leftover space. These views will get measured again down below if
// there is any leftover space.
if (isExactly) {
mTotalLength += lp.leftMargin + lp.rightMargin;
} else {
final int totalLength = mTotalLength;
mTotalLength = Math.max(totalLength, totalLength +
lp.leftMargin + lp.rightMargin);
}