- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) >
- JavaSwing基礎之Layout布局相關(guān)知識詳解
首先,還是從ViewRootImpl
說(shuō)起,界面的繪制會(huì )觸發(fā)performMeasure、performLayout
方法,而在performLayout
方法中就會(huì )調用mView的layout
方法開(kāi)始一層層View的布局工作。
private void performLayout(WindowManager.LayoutParams lp, int desiredWindowWidth, int desiredWindowHeight) { final View host = mView; host.layout(0, 0, host.getMeasuredWidth(), host.getMeasuredHeight()); }
mView
我們都知道了,就是頂層View——DecorView
,那么就進(jìn)去看看DecorView
的layout方法:
不好意思,DecorView中并沒(méi)有layout方法...
所以,我們直接看看View的layout
方法:
public void layout(int l, int t, int r, int b) { 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); } } protected void onLayout(boolean changed, int left, int top, int right, int bottom) { }
左、上、下、右
四個(gè)值。setOpticalFrame
方法或者setFrame
方法判斷布局參數是否改變。具體判斷過(guò)程就是通過(guò)老的上下左右值和新的上下左右值進(jìn)行比較,邏輯就在setFrame
方法中:
protected boolean setFrame(int left, int top, int right, int bottom) { boolean changed = false; if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) { changed = true; // Remember our drawn bit int drawn = mPrivateFlags & PFLAG_DRAWN; int oldWidth = mRight - mLeft; int oldHeight = mBottom - mTop; int newWidth = right - left; int newHeight = bottom - top; boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight); // Invalidate our old position invalidate(sizeChanged); mLeft = left; mTop = top; mRight = right; mBottom = bottom; mRenderNode.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom); } return changed; }
如果上下左右有一個(gè)參數值發(fā)生了改變,就說(shuō)明這個(gè)View的布局發(fā)生了改變,然后重新計算View的寬度高度(newWidth、newHeight),并賦值了View新的上下左右參數值。
在這個(gè)layout
方法中主要涉及到了四個(gè)參數:mLeft、mTop、mBottom、mRight
,分別代表了View的左坐標、上坐標、下坐標和右坐標,你可以把View理解為一個(gè)矩形,確定了這四個(gè)值,就能確定View矩形的四個(gè)頂點(diǎn)值,也就能確定View在畫(huà)布中的具體位置。
所以,layout
方法到底干了啥?
就是傳入上下左右值
、然后賦值上下左右值
、完畢。
然后我們就可以根據這些值獲取View的一系列參數,比如View寬度:
public final int getWidth() { return mRight - mLeft; }
至此,View的layout
方法就結束了,主要就是通過(guò)對上下左右參數的賦值完成對View的布局,非常簡(jiǎn)單。
下面看看ViewGroup
。
@Override public final void layout(int l, int t, int r, int b) { if (!mSuppressLayout && (mTransition == null || !mTransition.isChangingLayout())) { if (mTransition != null) { mTransition.layoutChange(this); } super.layout(l, t, r, b); } else { mLayoutCalledWhileSuppressed = true; } }
額,還是調用到View的layout
方法,難道說(shuō)ViewGroup
和View
的布局過(guò)程是一樣的,就是確定了本身的位置?
那ViewGroup
的子View怎么辦呢?不急,我們剛才說(shuō)layout
方法的時(shí)候還漏了一個(gè)onLayout
方法,只不過(guò)這個(gè)方法在View
里面是空實(shí)現,而到了ViewGroup
中變成了一個(gè)抽象方法:
@Override protected abstract void onLayout(boolean changed, int l, int t, int r, int b);
也就是任何ViewGroup
都必須實(shí)現這個(gè)方法,來(lái)完成對子View
的布局擺放。
具體的布局擺放邏輯就是在onLayout
方法中一個(gè)個(gè)調用子View
的layout方法,然后完成每個(gè)子View的布局,最終完成繪制工作。
接下來(lái)我們就來(lái)自己實(shí)現一個(gè)垂直線(xiàn)性布局(類(lèi)似LinearLayout)
,正好復習下上一節的onMearsure
和這一節的onLayout
。
首先,我們要確定我們這個(gè)自定義ViewGroup
的作用,是類(lèi)似垂直方向的LinearLayout
功能,在該ViewGroup下的子View
可以按垂直線(xiàn)性順序依次往下排放。我們給它起個(gè)名字叫VerticalLayout
~
繼承ViewGroup
首先,我們這個(gè)布局肯定要繼承自ViewGroup
,并且實(shí)現相應的構造方法:
public class VerticalLayout : ViewGroup { constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int = 0) : super( context, attrs, defStyleAttr ) constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) { } }
重寫(xiě)generateLayoutParams方法
自定義ViewGroup還需要重寫(xiě)的一個(gè)方法是generateLayoutParams,這一步是為了讓我們的ViewGroup支持Margin,后續我們就可以通過(guò)MarginLayoutParams來(lái)獲取子View的Margin值。
override fun generateLayoutParams(attrs: AttributeSet?): LayoutParams? { return MarginLayoutParams(context, attrs) }
重寫(xiě)測量方法onMeasure
然后,我們需要對我們的布局進(jìn)行測量,也就是重寫(xiě)onMeasure
方法。
在該方法中,我們需要對我們的布局進(jìn)行測量,并且將測量好的寬高傳入setMeasuredDimension
方法,完成測量。
protected final void setMeasuredDimension(int measuredWidth, int measuredHeight)
之前我們說(shuō)過(guò),onMeasure
方法會(huì )傳進(jìn)來(lái)兩個(gè)參數,widthMeasureSpec
和heightMeasureSpec
。
里面包含了父View根據當前View的LayoutParams
和父View的測量規格
進(jìn)行計算,得出的對當前View期望的測量模式和測量大小
:
也就是當寬或者高為確定值時(shí),那么當前布局View的寬高也就是設定為父View給我們設置好的測量大小即可。比如寬為400dp
,那么我們無(wú)需重新測量直接調用setMeasuredDimension
傳入這個(gè)固定值即可。
這時(shí)候,說(shuō)明父View對當前View的要求不固定,是可以為任意大小或者不超過(guò)最大值的情況,比如設置這個(gè)VerticalLayout
的高度為wrap_content
。那么我們就必須重新進(jìn)行高度測量了,因為只有我們設計者知道這個(gè)自適應高度需要怎么計算。具體就是VerticalLayout
是一個(gè)垂直線(xiàn)性布局,所以高度很自然就是所有子View的高度之和。
至此,onMeasure
方法的邏輯也基本摸清了:
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { super.onMeasure(widthMeasureSpec, heightMeasureSpec) //獲取寬高的測量模式和測量大小 val widthMode = MeasureSpec.getMode(widthMeasureSpec) val heightMode = MeasureSpec.getMode(heightMeasureSpec) val sizeWidth = MeasureSpec.getSize(widthMeasureSpec) val sizeHeight = MeasureSpec.getSize(heightMeasureSpec) var mHeight = 0 var mWidth = 0 //遍歷子View,獲取總高度 for (i in 0 until childCount) { val childView = getChildAt(i) //測量子View的寬和高 measureChild(childView, widthMeasureSpec, heightMeasureSpec) val lp = childView.layoutParams as MarginLayoutParams val childWidth = childView.measuredWidth + lp.leftMargin + lp.rightMargin val childHeight = childView.measuredHeight + lp.topMargin + lp.bottomMargin //計算得出最大寬度 mWidth = Math.max(mWidth, childWidth) //累計計算高度 mHeight += childHeight } //設置寬高 setMeasuredDimension( if (widthMode == MeasureSpec.EXACTLY) sizeWidth else mWidth, if (heightMode == MeasureSpec.EXACTLY) sizeHeight else mHeight ) }
主要的邏輯就是遍歷子View,得出VerticalLayout
的實(shí)際寬高:
最后調用setMeasuredDimension
根據測量模式 傳入寬高。
重寫(xiě)布局方法onLayout
上文說(shuō)過(guò),作為一個(gè)ViewGroup
,必須重寫(xiě)onLayout
方法,來(lái)保證子View的正常布局擺放。
垂直線(xiàn)性布局VerticalLayout
亦是如此,那么在這個(gè)布局中onLayout
方法的關(guān)鍵邏輯又是什么呢?
還是那句話(huà),確定位置,也就是確定左、上、右、下
四個(gè)參數值,而在VerticalLayout
中,最關(guān)鍵的參數就是這個(gè)上,也就是top值
。
每個(gè)View的top
值必須是上一個(gè)View的bottom值,也就是接著(zhù)上一個(gè)View進(jìn)行擺放,這樣才會(huì )是垂直線(xiàn)性
的效果,所以我們需要做的就是動(dòng)態(tài)計算每個(gè)View的top
值,其實(shí)也就是不斷累加View的高度,作為下一個(gè)View的top值。
override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) { var childWidth = 0 var childHeight = 0 var childTop = 0 var lp: MarginLayoutParams //遍歷子View,布局每個(gè)子View for (i in 0 until childCount) { val childView = getChildAt(i) childHeight = childView.measuredHeight childWidth = childView.measuredWidth lp = childView.layoutParams as MarginLayoutParams //累計計算top值 childTop += lp.topMargin //布局子View childView.layout( lp.leftMargin, childTop, lp.leftMargin + childWidth, childTop + childHeight ); childTop += childHeight + lp.bottomMargin } }
邏輯還是挺簡(jiǎn)單的,
left
是固定的子View的leftMargin。top
是累加計算的子View的高度 + Margin值。right
是left + 子View的寬度。bottom
是top + 子View的高度。最后調用子View的layout方法,對每個(gè)子View進(jìn)行布局。
大功告成,最后看看我們這個(gè)自定義垂直線(xiàn)性布局的效果吧~
<com.panda.studynote3.VerticalLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:layout_width="100dp" android:layout_height="100dp" android:text="啦啦啦" android:textSize="20sp" android:textColor="@color/white" android:background="@color/design_default_color_primary" /> <TextView android:layout_width="300dp" android:layout_height="200dp" android:layout_marginTop="20dp" android:background="@color/cardview_dark_background" android:textSize="20sp" android:textColor="@color/white" android:text="你好啊" /> <TextView android:layout_width="140dp" android:layout_height="100dp" android:text="嘻嘻" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:textSize="20sp" android:gravity="center" android:textColor="@color/black" android:background="@color/teal_200" /> </com.panda.studynote3.VerticalLayout>
到此這篇關(guān)于Java基礎之Layout布局相關(guān)知識詳解的文章就介紹到這了,更多相關(guān)Java Layout布局內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。
Copyright ? 2009-2021 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 珠海市特網(wǎng)科技有限公司 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站