int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
int modeHeight = MeasureSpec.getMode(heightMeasureSpec);
// 如果是warp_content情況下,記錄寬和高
int width = 0;
int height = 0;
//記錄每一行的寬度,width不斷取最大寬度
int lineWidth = 0;
//每一行的高度,累加至height
int lineHeight = 0;
int count = getChildCount();
int left = getPaddingLeft();
int top = getPaddingTop();
// 遍歷每個子元素
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
if (child.getVisibility() == GONE)
continue;
// 測量每一個child的寬和高
measureChild(child, widthMeasureSpec, heightMeasureSpec);
// 得到child的lp
ViewGroup.LayoutParams lp = child.getLayoutParams();
// 當(dāng)前子空間實際占據(jù)的寬度
int childWidth = child.getMeasuredWidth() + childHorizontalSpace;
// 當(dāng)前子空間實際占據(jù)的高度
int childHeight = child.getMeasuredHeight() + childVerticalSpace;
if (lp != null && lp instanceof MarginLayoutParams) {
MarginLayoutParams params = (MarginLayoutParams) lp;
childWidth += params.leftMargin + params.rightMargin;
childHeight += params.topMargin + params.bottomMargin;
}
//如果加入當(dāng)前child,則超出最大寬度,則的到目前最大寬度給width,類加height 然后開啟新行
if (lineWidth + childWidth > sizeWidth - getPaddingLeft() - getPaddingRight()) {
width = Math.max(lineWidth, childWidth);// 取最大的
lineWidth = childWidth; // 重新開啟新行,開始記錄
// 疊加當(dāng)前高度,
height += lineHeight;
// 開啟記錄下一行的高度
lineHeight = childHeight;
child.setTag(new Location(left, top + height, childWidth + left - childHorizontalSpace, height + child.getMeasuredHeight() + top));
} else {
// 否則累加值lineWidth,lineHeight取最大高度
child.setTag(new Location(lineWidth + left, top + height, lineWidth + childWidth - childHorizontalSpace + left, height + child.getMeasuredHeight() + top));
lineWidth += childWidth;
lineHeight = Math.max(lineHeight, childHeight);
}
}
width = Math.max(width, lineWidth) + getPaddingLeft() + getPaddingRight();
height += lineHeight;
sizeHeight += getPaddingTop() + getPaddingBottom();
height += getPaddingTop() + getPaddingBottom();
setMeasuredDimension((modeWidth == MeasureSpec.EXACTLY) ? sizeWidth : width, (modeHeight == MeasureSpec.EXACTLY) ? sizeHeight : height);
}
/**
- 記錄子控件的坐標(biāo)
*/
public class Location {
public Location(int left, int top, int right, int bottom) {
this.left = left;
this.top = top;
this.right = right;
this.bottom = bottom;
}
public int left;
public int top;
public int right;
public int bottom;
}
//計算當(dāng)前View以及子View的位置
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
MLog.e(getClass().getName(),“onLayout”);
//獲取子View個數(shù)
int count = getChildCount();
for (int i = 0; i < count; i++) {
//獲取子View
View child = getChildAt(i);
//判斷是否顯示
if (child.getVisibility() == GONE)
continue;
//獲取子View的坐標(biāo)
Location location = (Location) child.getTag();
//設(shè)置子View位置
child.layout(location.left, location.top, location.right, location.bottom);
}
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
MLog.e(getClass().getName(),“onSizeChanged”);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
MLog.e(getClass().getName(),“onDraw”);
}
}
2.使用自定義CustomLayout
<?xml version="1.0" encoding="utf-8"?><com.scc.demo.view.CustomLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:custom=“http://schemas.android.com/apk/res-auto”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:layout_margin=“@dimen/dimen_20”
custom:horizontalSpace=“10dp”
custom:verticalSpace=“20dp”>
<TextView
style=“@style/TvStyle”
android:text=“破陣子·為陳同甫賦壯詞以寄” />
<TextView
style=“@style/TvStyle”
android:text=“宋·辛棄疾” />
<TextView
style=“@style/TvStyle”
android:text=“醉里挑燈看劍” />
<TextView
style=“@style/TvStyle”
android:text=“夢回吹角連營” />
<TextView
style=“@style/TvStyle”
android:text=“八百里分麾下炙” />
<TextView
style=“@style/TvStyle”
android:text=“五十弦翻塞外聲” />
<TextView
style=“@style/TvStyle”
android:text=“沙場秋點兵” />
<TextView
style=“@style/TvStyle”
android:text=“馬作的盧飛快” />
<TextView
style=“@style/TvStyle”
android:text=“弓如霹靂弦驚(增加點長度)” />
<TextView
style=“@style/TvStyle”
android:text=“了卻君王天下事” />
<TextView
style=“@style/TvStyle”
android:text=“贏得生前身后名” />
<TextView
style=“@style/TvStyle”
android:text=“可憐白發(fā)生!” />
</com.scc.demo.view.CustomLayout>
自定義屬性
在app/src/main/res/values/attrs.xml中添加屬性
<?xml version="1.0" encoding="utf-8"?>使用自定義屬性
- 在xml中使用
一定要添加:xmlns:test=”schemas.android.com/apk/res-aut…
<com.scc.demo.view.CustomLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:custom=“http://schemas.android.com/apk/res-auto”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:layout_margin=“@dimen/dimen_20”
custom:horizontalSpace=“10dp”
custom:verticalSpace=“20dp”>
</com.scc.demo.view.CustomLayout>
自我介紹一下,小編13年上海交大畢業(yè),曾經(jīng)在小公司待過,也去過華為、OPPO等大廠,18年進入阿里一直到現(xiàn)在。
深知大多數(shù)Android工程師,想要提升技能,往往是自己摸索成長或者是報班學(xué)習(xí),但對于培訓(xùn)機構(gòu)動則幾千的學(xué)費,著實壓力不小。自己不成體系的自學(xué)效果低效又漫長,而且極易碰到天花板技術(shù)停滯不前!
因此收集整理了一份《2024年Android移動開發(fā)全套學(xué)習(xí)資料》,初衷也很簡單,就是希望能夠幫助到想自學(xué)提升又不知道該從何學(xué)起的朋友,同時減輕大家的負擔(dān)。
既有適合小白學(xué)習(xí)的零基礎(chǔ)資料,也有適合3年以上經(jīng)驗的小伙伴深入學(xué)習(xí)提升的進階課程,基本涵蓋了95%以上Android開發(fā)知識點,真正體系化!
由于文件比較大,這里只是將部分目錄大綱截圖出來,每個節(jié)點里面都包含大廠面經(jīng)、學(xué)習(xí)筆記、源碼講義、實戰(zhàn)項目、講解視頻,并且后續(xù)會持續(xù)更新
如果你覺得這些內(nèi)容對你有幫助,可以添加V獲取:vip204888 (備注Android)
最后
由于文章篇幅原因,我只把面試題列了出來,詳細的答案,我整理成了一份PDF文檔,這份文檔還包括了還有?高級架構(gòu)技術(shù)進階腦圖、Android開發(fā)面試專題資料,高級進階架構(gòu)資料 ,幫助大家學(xué)習(xí)提升進階,也節(jié)省大家在網(wǎng)上搜索資料的時間來學(xué)習(xí)。文章來源:http://www.zghlxwxcb.cn/news/detail-853049.html
mg.cn/13f2cb2e05a14868a3f0fd6ac81d625c.png)
既有適合小白學(xué)習(xí)的零基礎(chǔ)資料,也有適合3年以上經(jīng)驗的小伙伴深入學(xué)習(xí)提升的進階課程,基本涵蓋了95%以上Android開發(fā)知識點,真正體系化!
由于文件比較大,這里只是將部分目錄大綱截圖出來,每個節(jié)點里面都包含大廠面經(jīng)、學(xué)習(xí)筆記、源碼講義、實戰(zhàn)項目、講解視頻,并且后續(xù)會持續(xù)更新
如果你覺得這些內(nèi)容對你有幫助,可以添加V獲?。簐ip204888 (備注Android)
[外鏈圖片轉(zhuǎn)存中…(img-cP86rkm4-1711917793602)]
最后
由于文章篇幅原因,我只把面試題列了出來,詳細的答案,我整理成了一份PDF文檔,這份文檔還包括了還有?高級架構(gòu)技術(shù)進階腦圖、Android開發(fā)面試專題資料,高級進階架構(gòu)資料 ,幫助大家學(xué)習(xí)提升進階,也節(jié)省大家在網(wǎng)上搜索資料的時間來學(xué)習(xí)。
本文已被CODING開源項目:《Android學(xué)習(xí)筆記總結(jié)+移動架構(gòu)視頻+大廠面試真題+項目實戰(zhàn)源碼》收錄文章來源地址http://www.zghlxwxcb.cn/news/detail-853049.html
到了這里,關(guān)于Android基礎(chǔ)到進階UI祖父級 ViewGroup介紹+實用的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!