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ā)生了改變,然后重新計(jì)算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)行計(jì)算,得出的對當(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)行高度測量了,因?yàn)橹挥形覀冊O(shè)計(jì)者知道這個自適應(yīng)高度需要怎么計(jì)算。具體就是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
//計(jì)算得出最大寬度
mWidth = Math.max(mWidth, childWidth)
//累計(jì)計(jì)算高度
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)計(jì)算每個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
//累計(jì)計(jì)算top值
childTop += lp.topMargin
//布局子View
childView.layout(
lp.leftMargin,
childTop,
lp.leftMargin + childWidth,
childTop + childHeight
);
childTop += childHeight + lp.bottomMargin
}
}
邏輯還是挺簡單的,
left是固定的子View的leftMargin。top是累加計(jì)算的子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)文章希望大家以后多多支持腳本之家!
- 基于JavaSwing+mysql開發(fā)一個學(xué)生社團(tuán)管理系統(tǒng)設(shè)計(jì)和實(shí)現(xiàn)
- MySQL深分頁問題解決的實(shí)戰(zhàn)記錄
- MySQL連接控制插件介紹
- MySQL中l(wèi)imit對查詢語句性能的影響
- 基于JavaSwing設(shè)計(jì)和實(shí)現(xiàn)的酒店管理系統(tǒng)
- JavaSwing坦克大戰(zhàn)游戲的設(shè)計(jì)和實(shí)現(xiàn)
- JavaSwing后臺播放音樂mp3
- 基于Mysql+JavaSwing的超市商品管理系統(tǒng)設(shè)計(jì)與實(shí)現(xiàn)
相關(guān)文章
SpringBoot上傳文件并配置本地資源映射來訪問文件的實(shí)例代碼
這篇文章主要介紹了SpringBoot上傳文件并配置本地資源映射來訪問文件的實(shí)例代碼,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04
Java的Hibernate框架中集合類數(shù)據(jù)結(jié)構(gòu)的映射編寫教程
Hibernate可以將Java中幾個內(nèi)置的集合結(jié)構(gòu)映射為數(shù)據(jù)庫使用的關(guān)系模型,下面我們就來看一下Java的Hibernate框架中集合類數(shù)據(jù)結(jié)構(gòu)的映射編寫教程:2016-07-07
Java多線程編程小實(shí)例模擬停車場系統(tǒng)
這是一個關(guān)于Java多線程編程的例子,用多線程的思想模擬停車場管理系統(tǒng),這里分享給大家,供需要的朋友參考。2017-10-10
rabbitmq五種模式詳解(含實(shí)現(xiàn)代碼)
這篇文章主要介紹了rabbitmq五種模式詳解(含實(shí)現(xiàn)代碼),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04
java程序員自己的圖片轉(zhuǎn)文字OCR識圖工具分享
這篇文章主要介紹了java程序員自己的圖片轉(zhuǎn)文字OCR識圖工具,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11

