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

難點(diǎn)
相比于自定義 View ,自定義 ViewGroup 的難點(diǎn)在于,子控件位置的確定和布局大小的確定。不像 單個(gè) View 子要花粉好模式,測(cè)量好寬度就搞定了,ViewGroup 的長(zhǎng)寬根據(jù)子 View 的數(shù)量和單個(gè)的大小變化而變化。這就是最大的坎,所以該如何確定 ViewGroup 的大小呢?
步驟
這里 我為大家設(shè)計(jì)一個(gè) 類似 LinearLayout 線性布局的 ViewGroup 作為范例。
首先,如果是一個(gè) LinearLayout 那么當(dāng)設(shè)置 wrap_content 時(shí),他就會(huì)以子空間中最寬的那個(gè)為它的寬度。同時(shí)在高度方面會(huì)是所有子控件高度的總和。所以我們先寫(xiě)兩個(gè)方法,分別用于測(cè)量 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;
}
對(duì)于 ViewGroup 而言我們可以粗略的分為兩種模式:固定長(zhǎng)寬模式(match_parent),自適應(yīng)模式(wrap_content),根據(jù)這兩種模式,就可以對(duì) ViewGroup 的繪制進(jìn)行劃分。這里關(guān)于 measureChildren 這個(gè)方法,他是用于將所有的子 View 進(jìn)行測(cè)量,這會(huì)觸發(fā)每個(gè)子 View 的 onMeasure 函數(shù),但是大家要注意要與 measureChild 區(qū)分,measureChild 是對(duì)單個(gè) view 進(jìn)行測(cè)量
@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());
}
}
重寫(xiě) onLayout
整完上面這些東西,我們的布局大小七十九已經(jīng)出來(lái)了,然我們?cè)诨顒?dòng)的布局文件里面加上它,并添加上幾個(gè)子 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)行效果如下:

我們看見(jiàn)布局出來(lái)了,大小好像也沒(méi)啥問(wèn)題,但是子 View 呢??! 這么沒(méi)看見(jiàn)子 View 在看看代碼,系統(tǒng)之前然我們重寫(xiě)的 onLayout() 還是空著的呀??!也就是說(shuō),子 View 的大小和位置根本就還沒(méi)有進(jìn)行過(guò)設(shè)定!讓我們來(lái)重寫(xiě)下 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é)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Android開(kāi)發(fā)手冊(cè)Chip監(jiān)聽(tīng)及ChipGroup監(jiān)聽(tīng)
這篇文章主要為大家介紹了Android開(kāi)發(fā)手冊(cè)Chip監(jiān)聽(tīng)及ChipGroup監(jiān)聽(tīng),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
Android編程實(shí)現(xiàn)在底端顯示選項(xiàng)卡的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)在底端顯示選項(xiàng)卡的方法,涉及Android界面線性布局、相對(duì)布局及選項(xiàng)卡設(shè)置相關(guān)操作技巧,需要的朋友可以參考下2017-02-02
Android編程視頻播放API之MediaPlayer用法示例
這篇文章主要介紹了Android編程視頻播放API之MediaPlayer用法,結(jié)合實(shí)例形式分析了基于Android API實(shí)現(xiàn)視頻播放功能的多媒體文件讀取、判斷、事件響應(yīng)及流媒體播放等相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-08-08
Android中ArrayList和數(shù)組相互轉(zhuǎn)換
在我們?nèi)粘i_(kāi)發(fā)中難免會(huì)要將ArrayList和數(shù)組相互轉(zhuǎn)換,那么如何才能相互轉(zhuǎn)換呢?下面跟著小編一起通過(guò)這篇文章學(xué)習(xí)學(xué)習(xí)。2016-08-08
Android 三種動(dòng)畫(huà)詳解及簡(jiǎn)單實(shí)例
這篇文章主要介紹了Android 三種動(dòng)畫(huà)詳解及簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-04-04
Android 實(shí)現(xiàn)左滑出現(xiàn)刪除選項(xiàng)
滑動(dòng)刪除的部分主要包含兩個(gè)部分, 一個(gè)是內(nèi)容區(qū)域(用于放置正常顯示的view),另一個(gè)是操作區(qū)域(用于放置刪除按鈕)。下面通過(guò)本文給大家介紹Android 實(shí)現(xiàn)左滑出現(xiàn)刪除選項(xiàng),需要的朋友可以參考下2017-06-06
Android 自定義view實(shí)現(xiàn)TopBar效果
這篇文章主要為大家詳細(xì)介紹了Android 自定義view實(shí)現(xiàn)TopBar效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
Kotlin實(shí)現(xiàn)在類里面創(chuàng)建main函數(shù)
這篇文章主要介紹了Kotlin實(shí)現(xiàn)在類里面創(chuàng)建main函數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03

