<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                #### 4.3.2 layout過程 Layout的作用是ViewGroup用來確定子元素的位置,當ViewGroup的位置被確定后,它在onLayout中會遍歷所有的子元素并調用其layout方法,在layout方法中onLayout方法又會被調用。Layout過程和measure過程相比就簡單多了,layout方法確定View本身的位置,而onLayout方法則會確定所有子元素的位置,先看View的layout方法,如下所示。 public void layout(int l, int t, int r, int b) { if ((mPrivateFlags3 & PFLAG3_MEASURE_NEEDED_BEFORE_LAYOUT) ! = 0) { onMeasure(mOldWidthMeasureSpec, mOldHeightMeasureSpec); mPrivateFlags3 &= ~PFLAG3_MEASURE_NEEDED_BEFORE_LAYOUT; } int oldL = mLeft; int oldT = mTop; int oldB = mBottom; int oldR = mRight; boolean changed = isLayoutModeOptical(mParent) ? setOpticalFrame(l, t, r, b) : setFrame(l, t, r, b); if (changed || (mPrivateFlags & PFLAG_LAYOUT_REQUIRED) == PFLAG_ LAYOUT_REQUIRED) { onLayout(changed, l, t, r, b); mPrivateFlags &= ~PFLAG_LAYOUT_REQUIRED; ListenerInfo li = mListenerInfo; if (li ! = null && li.mOnLayoutChangeListeners ! = null) { ArrayList<OnLayoutChangeListener> listenersCopy = (ArrayList<OnLayoutChangeListener>)li.mOnLayout- ChangeListeners.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); } } } mPrivateFlags &= ~PFLAG_FORCE_LAYOUT; mPrivateFlags3 |= PFLAG3_IS_LAID_OUT; } layout方法的大致流程如下:首先會通過setFrame方法來設定View的四個頂點的位置,即初始化mLeft、mRight、mTop和mBottom這四個值,View的四個頂點一旦確定,那么View在父容器中的位置也就確定了;接著會調用onLayout方法,這個方法的用途是父容器確定子元素的位置,和onMeasure方法類似,onLayout的具體實現同樣和具體的布局有關,所以View和ViewGroup均沒有真正實現onLayout方法。接下來,我們可以看一下LinearLayout的onLayout方法,如下所示。 protected void onLayout(boolean changed, int l, int t, int r, int b) { if (mOrientation == VERTICAL) { layoutVertical(l, t, r, b); } else { layoutHorizontal(l, t, r, b); } } LinearLayout中onLayout的實現邏輯和onMeasure的實現邏輯類似,這里選擇layoutVertical繼續講解,為了更好地理解其邏輯,這里只給出了主要的代碼: void layoutVertical(int left, int top, int right, int bottom) { ... final int count = getVirtualChildCount(); for (int i = 0; i < count; i++) { final View child = getVirtualChildAt(i); if (child == null) { childTop += measureNullChild(i); } else if (child.getVisibility() ! = GONE) { final int childWidth = child.getMeasuredWidth(); final int childHeight = child.getMeasuredHeight(); final LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) child.getLayoutParams(); ... if (hasDividerBeforeChildAt(i)) { childTop += mDividerHeight; } childTop += lp.topMargin; setChildFrame(child, childLeft, childTop + getLocationOffset (child), childWidth, childHeight); childTop += childHeight + lp.bottomMargin + getNextLocation- Offset(child); i += getChildrenSkipCount(child, i); } } } 這里分析一下layoutVertical的代碼邏輯,可以看到,此方法會遍歷所有子元素并調用setChildFrame方法來為子元素指定對應的位置,其中childTop會逐漸增大,這就意味著后面的子元素會被放置在靠下的位置,這剛好符合豎直方向的LinearLayout的特性。至于setChildFrame,它僅僅是調用子元素的layout方法而已,這樣父元素在layout方法中完成自己的定位以后,就通過onLayout方法去調用子元素的layout方法,子元素又會通過自己的layout方法來確定自己的位置,這樣一層一層地傳遞下去就完成了整個View樹的layout過程。setChildFrame方法的實現如下所示。 private void setChildFrame(View child, int left, int top, int width, int height) { child.layout(left, top, left + width, top + height); } 我們注意到,setChildFrame中的width和height實際上就是子元素的測量寬/高,從下面的代碼可以看出這一點: final int childWidth = child.getMeasuredWidth(); final int childHeight = child.getMeasuredHeight(); setChildFrame(child, childLeft, childTop + getLocationOffset(child), childWidth, childHeight); 而在layout方法中會通過setFrame去設置子元素的四個頂點的位置,在setFrame中有如下幾句賦值語句,這樣一來子元素的位置就確定了: mLeft = left; mTop = top; mRight = right; mBottom = bottom; 下面我們來回答一個在4.3.2節中提到的問題:View的測量寬/高和最終/寬高有什么區別?這個問題可以具體為:View的getMeasuredWidth和getWidth這兩個方法有什么區別,至于getMeasuredHeight和getHeight的區別和前兩者完全一樣。為了回答這個問題,首先,我們看一下getwidth和getHeight這兩個方法的具體實現: public final int getWidth() { return mRight - mLeft; } public final int getHeight() { return mBottom - mTop; } 從getWidth和getHeight的源碼再結合mLeft、mRight、mTop和mBottom這四個變量的賦值過程來看,getWidth方法的返回值剛好就是View的測量寬度,而getHeight方法的返回值也剛好就是View的測量高度。經過上述分析,現在我們可以回答這個問題了:在View的默認實現中,View的測量寬/高和最終寬/高是相等的,只不過測量寬/高形成于View的measure過程,而最終寬/高形成于View的layout過程,即兩者的賦值時機不同,測量寬/高的賦值時機稍微早一些。因此,在日常開發中,我們可以認為View的測量寬/高就等于最終寬/高,但是的確存在某些特殊情況會導致兩者不一致,下面舉例說明。 如果重寫View的layout方法,代碼如下: public void layout(int l, int t, int r, int b) { super.layout(l, t, r + 100, b + 100); } 上述代碼會導致在任何情況下View的最終寬/高總是比測量寬/高大100px,雖然這樣做會導致View顯示不正常并且也沒有實際意義,但是這證明了測量寬/高的確可以不等于最終寬/高。另外一種情況是在某些情況下,View需要多次measure才能確定自己的測量寬/高,在前幾次的測量過程中,其得出的測量寬/高有可能和最終寬/高不一致,但最終來說,測量寬/高還是和最終寬/高相同。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看