Android進(jìn)階教程之ViewGroup自定義布局
前言
在我們的實(shí)際應(yīng)用中, 經(jīng)常需要用到自定義控件,比如自定義圓形頭像,自定義計(jì)步器等等。但有時我們不僅需要自定義控件,舉個例子,F(xiàn)loatingActionButton 大家都很常用,所以大家也很經(jīng)常會有一種需求,點(diǎn)擊某個 FloatingActionButton 彈出更多 FloatingActionButton ,這個需求的一般思路是寫 n 個 button 然后再一個個的去設(shè)置動畫效果。但這實(shí)在是太麻煩了,所以網(wǎng)上有個 FloatingActionButtonMenu 這個開源庫,這就是利用到了自定義布局 「ViewGroup」,現(xiàn)在就讓我給他家介紹下,如何自定義布局 「layout」。

難點(diǎn)
相比于自定義 View ,自定義 ViewGroup 的難點(diǎn)在于,子控件位置的確定和布局大小的確定。不像 單個 View 子要花粉好模式,測量好寬度就搞定了,ViewGroup 的長寬根據(jù)子 View 的數(shù)量和單個的大小變化而變化。這就是最大的坎,所以該如何確定 ViewGroup 的大小呢?
步驟
這里 我為大家設(shè)計(jì)一個 類似 LinearLayout 線性布局的 ViewGroup 作為范例。
首先,如果是一個 LinearLayout 那么當(dāng)設(shè)置 wrap_content 時,他就會以子空間中最寬的那個為它的寬度。同時在高度方面會是所有子控件高度的總和。所以我們先寫兩個方法,分別用于測量 ViewGroup 的寬度和高度。
private int getMaxWidth(){
int count = getChildCount();
int maxWidth = 0;
for (int i = 0 ; i < count ; i ++){
int currentWidth = getChildAt(i).getMeasuredWidth();
if (maxWidth < currentWidth){
maxWidth = currentWidth;
}
}
return maxWidth;
}
private int getTotalHeight(){
int count = getChildCount();
int totalHeight = 0;
for (int i = 0 ; i < count ; i++){
totalHeight += getChildAt(i).getMeasuredHeight();
}
return totalHeight;
}
對于 ViewGroup 而言我們可以粗略的分為兩種模式:固定長寬模式(match_parent),自適應(yīng)模式(wrap_content),根據(jù)這兩種模式,就可以對 ViewGroup 的繪制進(jìn)行劃分。這里關(guān)于 measureChildren 這個方法,他是用于將所有的子 View 進(jìn)行測量,這會觸發(fā)每個子 View 的 onMeasure 函數(shù),但是大家要注意要與 measureChild 區(qū)分,measureChild 是對單個 view 進(jìn)行測量
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
measureChildren(widthMeasureSpec, heightMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int heightMode= MeasureSpec.getMode(heightMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
if (widthMode == MeasureSpec.AT_MOST && heightMode == MeasureSpec.AT_MOST){
int groupWidth = getMaxWidth();
int groupHeight= getTotalHeight();
setMeasuredDimension(groupWidth, groupHeight);
}else if (widthMode == MeasureSpec.AT_MOST){
setMeasuredDimension(getMaxWidth(), height);
}else if (heightMode == MeasureSpec.AT_MOST){
setMeasuredDimension(width, getTotalHeight());
}
}
重寫 onLayout
整完上面這些東西,我們的布局大小七十九已經(jīng)出來了,然我們在活動的布局文件里面加上它,并添加上幾個子 View 然后運(yùn)行一下,先看看效果:
<com.entry.android_view_user_defined_first.views.MyLinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/colorAccent"> <Button android:layout_width="100dp" android:layout_height="50dp" android:text="qwe"/> <Button android:layout_width="250dp" android:layout_height="150dp" android:text="qwe"/> <Button android:layout_width="200dp" android:layout_height="75dp" android:text="qwe"/> </com.entry.android_view_user_defined_first.views.MyLinearLayout>
運(yùn)行效果如下:

我們看見布局出來了,大小好像也沒啥問題,但是子 View 呢??! 這么沒看見子 View 在看看代碼,系統(tǒng)之前然我們重寫的 onLayout() 還是空著的呀??!也就是說,子 View 的大小和位置根本就還沒有進(jìn)行過設(shè)定!讓我們來重寫下 onLayout() 方法。
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int count = getChildCount();
int currentHeight = 0;
for (int i = 0 ; i < count ; i++){
View view = getChildAt(i);
int height = view.getMeasuredHeight();
int width = view.getMeasuredWidth();
view.layout(l, currentHeight, l + width, currentHeight + height);
currentHeight += height;
}
}
再運(yùn)行一下看看:

成功了有木有!
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。
相關(guān)文章
Android List(集合)中的對象以某一個字段排序案例
這篇文章主要介紹了Android List(集合)中的對象以某一個字段排序案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
Android開發(fā)之TabActivity用法實(shí)例詳解
這篇文章主要介紹了Android開發(fā)之TabActivity用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android擴(kuò)展Activity實(shí)現(xiàn)標(biāo)簽頁效果的具體步驟與相關(guān)技巧,需要的朋友可以參考下2016-03-03
Android?webView加載數(shù)據(jù)時內(nèi)存溢出問題及解決
這篇文章主要介紹了Android?webView加載數(shù)據(jù)時內(nèi)存溢出問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12
Android應(yīng)用動態(tài)修改主題的方法示例
今天小編就為大家分享一篇關(guān)于Android應(yīng)用動態(tài)修改主題的方法示例,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03
Android App安裝列表獲取方法(實(shí)踐方案)
文章介紹了Android 11及以上版本獲取應(yīng)用列表的方案調(diào)整,包括權(quán)限配置、白名單配置和action配置三種方式,并提供了相應(yīng)的Java和Kotlin代碼示例,建議在Android 15及以上版本中使用action方式獲取應(yīng)用列表,感興趣的朋友一起看看吧2025-03-03
android中圖片的三級緩存cache策略(內(nèi)存/文件/網(wǎng)絡(luò))
實(shí)現(xiàn)圖片緩存也不難,需要有相應(yīng)的cache策略。這里我采用 內(nèi)存-文件-網(wǎng)絡(luò) 三層cache機(jī)制,其中內(nèi)存緩存包括強(qiáng)引用緩存和軟引用緩存(SoftReference),其實(shí)網(wǎng)絡(luò)不算cache,這里姑且也把它劃到緩存的層次結(jié)構(gòu)中2013-06-06

