JavaSwing基礎(chǔ)之Layout布局相關(guān)知識詳解
一、View layout方法
首先,還是從ViewRootImpl
說起,界面的繪制會觸發(fā)performMeasure、performLayout
方法,而在performLayout
方法中就會調(diào)用mView的layout
方法開始一層層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中并沒有l(wèi)ayout方法...
所以,我們直接看看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) { }
- 首先,方法傳入了四個參數(shù),分別代表view的
左、上、下、右
四個值。 - 然后通過
setOpticalFrame
方法或者setFrame
方法判斷布局參數(shù)是否改變。
具體判斷過程就是通過老的上下左右值和新的上下左右值進(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; }
如果上下左右有一個參數(shù)值發(fā)生了改變,就說明這個View的布局發(fā)生了改變,然后重新計算View的寬度高度(newWidth、newHeight),并賦值了View新的上下左右參數(shù)值。
在這個layout
方法中主要涉及到了四個參數(shù):mLeft、mTop、mBottom、mRight
,分別代表了View的左坐標(biāo)、上坐標(biāo)、下坐標(biāo)和右坐標(biāo),你可以把View理解為一個矩形,確定了這四個值,就能確定View矩形的四個頂點(diǎn)值,也就能確定View在畫布中的具體位置。
所以,layout
方法到底干了啥?
就是傳入上下左右值
、然后賦值上下左右值
、完畢。
然后我們就可以根據(jù)這些值獲取View的一系列參數(shù),比如View寬度:
public final int getWidth() { return mRight - mLeft; }
至此,View的layout
方法就結(jié)束了,主要就是通過對上下左右參數(shù)的賦值完成對View的布局,非常簡單。
下面看看ViewGroup
。
二、ViewGroup layout方法
@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; } }
額,還是調(diào)用到View的layout
方法,難道說ViewGroup
和View
的布局過程是一樣的,就是確定了本身的位置?
那ViewGroup
的子View怎么辦呢?不急,我們剛才說layout
方法的時候還漏了一個onLayout
方法,只不過這個方法在View
里面是空實(shí)現(xiàn),而到了ViewGroup
中變成了一個抽象方法:
@Override protected abstract void onLayout(boolean changed, int l, int t, int r, int b);
也就是任何ViewGroup
都必須實(shí)現(xiàn)這個方法,來完成對子View
的布局?jǐn)[放。
具體的布局?jǐn)[放邏輯就是在onLayout
方法中一個個調(diào)用子View
的layout方法,然后完成每個子View的布局,最終完成繪制工作。
接下來我們就來自己實(shí)現(xiàn)一個垂直線性布局(類似LinearLayout)
,正好復(fù)習(xí)下上一節(jié)的onMearsure
和這一節(jié)的onLayout
。
三、自定義垂直布局VerticalLayout
首先,我們要確定我們這個自定義ViewGroup
的作用,是類似垂直方向的LinearLayout
功能,在該ViewGroup下的子View
可以按垂直線性順序依次往下排放。我們給它起個名字叫VerticalLayout
~
繼承ViewGroup
首先,我們這個布局肯定要繼承自ViewGroup
,并且實(shí)現(xiàn)相應(yīng)的構(gòu)造方法:
public class VerticalLayout : ViewGroup { constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int = 0) : super( context, attrs, defStyleAttr ) constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) { } }
重寫generateLayoutParams方法
自定義ViewGroup還需要重寫的一個方法是generateLayoutParams,這一步是為了讓我們的ViewGroup支持Margin,后續(xù)我們就可以通過MarginLayoutParams來獲取子View的Margin值。
override fun generateLayoutParams(attrs: AttributeSet?): LayoutParams? { return MarginLayoutParams(context, attrs) }
重寫測量方法onMeasure
然后,我們需要對我們的布局進(jìn)行測量,也就是重寫onMeasure
方法。
在該方法中,我們需要對我們的布局進(jìn)行測量,并且將測量好的寬高傳入setMeasuredDimension
方法,完成測量。
protected final void setMeasuredDimension(int measuredWidth, int measuredHeight)
之前我們說過,onMeasure
方法會傳進(jìn)來兩個參數(shù),widthMeasureSpec
和heightMeasureSpec
。
里面包含了父View根據(jù)當(dāng)前View的LayoutParams
和父View的測量規(guī)格
進(jìn)行計算,得出的對當(dāng)前View期望的測量模式和測量大小
:
- 當(dāng)測量模式為MeasureSpec.EXACTLY
也就是當(dāng)寬或者高為確定值時,那么當(dāng)前布局View的寬高也就是設(shè)定為父View給我們設(shè)置好的測量大小即可。比如寬為400dp
,那么我們無需重新測量直接調(diào)用setMeasuredDimension
傳入這個固定值即可。
- 當(dāng)測量模式為MeasureSpec.AT_MOST 或者 UNSPECIFIED:
這時候,說明父View對當(dāng)前View的要求不固定,是可以為任意大小或者不超過最大值的情況,比如設(shè)置這個VerticalLayout
的高度為wrap_content
。那么我們就必須重新進(jìn)行高度測量了,因為只有我們設(shè)計者知道這個自適應(yīng)高度需要怎么計算。具體就是VerticalLayout
是一個垂直線性布局,所以高度很自然就是所有子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 } //設(shè)置寬高 setMeasuredDimension( if (widthMode == MeasureSpec.EXACTLY) sizeWidth else mWidth, if (heightMode == MeasureSpec.EXACTLY) sizeHeight else mHeight ) }
主要的邏輯就是遍歷子View,得出VerticalLayout
的實(shí)際寬高:
- 最終ViewGroup的高 = 所有子View的 (高 + margin值)
- 最終ViewGroup的寬 = 最大子View的 (寬 + margin值)
最后調(diào)用setMeasuredDimension
根據(jù)測量模式 傳入寬高。
重寫布局方法onLayout
上文說過,作為一個ViewGroup
,必須重寫onLayout
方法,來保證子View的正常布局?jǐn)[放。
垂直線性布局VerticalLayout
亦是如此,那么在這個布局中onLayout
方法的關(guān)鍵邏輯又是什么呢?
還是那句話,確定位置,也就是確定左、上、右、下
四個參數(shù)值,而在VerticalLayout
中,最關(guān)鍵的參數(shù)就是這個上,也就是top值
。
每個View的top
值必須是上一個View的bottom值,也就是接著上一個View進(jìn)行擺放,這樣才會是垂直線性
的效果,所以我們需要做的就是動態(tài)計算每個View的top
值,其實(shí)也就是不斷累加View的高度,作為下一個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,布局每個子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 } }
邏輯還是挺簡單的,
left
是固定的子View的leftMargin。top
是累加計算的子View的高度 + Margin值。right
是left + 子View的寬度。bottom
是top + 子View的高度。
最后調(diào)用子View的layout方法,對每個子View進(jì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基礎(chǔ)之Layout布局相關(guān)知識詳解的文章就介紹到這了,更多相關(guān)Java Layout布局內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MyBatis動態(tài)SQL標(biāo)簽用法實(shí)例詳解
本文通過實(shí)例代碼給大家介紹了MyBatis動態(tài)SQL標(biāo)簽用法,非常不錯,具有參考借鑒價值,需要的朋友參考下吧2017-07-07PostMan如何傳參給@RequestBody(接受前端參數(shù))
這篇文章主要介紹了PostMan如何傳參給@RequestBody(接受前端參數(shù)),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10Mybatis中<if>和<choose>的區(qū)別及“=”判斷方式
這篇文章主要介紹了Mybatis中<if>和<choose>的區(qū)別及“=”判斷方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06Spring Data分頁與排序的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于Spring Data分頁與排序的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12Springcloud服務(wù)注冊consul客戶端過程解析
這篇文章主要介紹了Springcloud服務(wù)注冊consul客戶端過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-08-08Java線程安全問題小結(jié)_動力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了Java線程安全問題小結(jié)的相關(guān)資料,需要的朋友可以參考下2017-05-05