Android自定義ViewGroup實(shí)現(xiàn)流式布局
本文實(shí)例為大家分享了Android自定義ViewGroup實(shí)現(xiàn)流式布局的具體代碼,供大家參考,具體內(nèi)容如下
1.概述
本篇給大家?guī)硪粋€實(shí)例,FlowLayout,什么是FlowLayout,我們常在App 的搜索界面看到熱門搜索詞,就是FlowLayout,我們要實(shí)現(xiàn)的就是圖中的效果,就是根據(jù)容器的寬,往容器里面添加元素,如果剩余的控件不足時候,自行添加到下一行,FlowLayout也叫流式布局,在開發(fā)中還是挺常用的.
2.對所有的子View進(jìn)行測量
onMeasure方法的調(diào)用次數(shù)是不確定的,所以為了避免測量出錯,需要把總的List集合,清空一下,一個View的繪制,需要經(jīng)過onMeasure方法的測量,和onLayout方法的排版才能顯示出來,在測量的方法中,我們把該ViewGroup中的所有子View遍歷出來,添加到一行中的List集合中,再把一行中的所有的元素集合添加到總的集合中去,并對每個子View元素進(jìn)行測量,測量的參數(shù),我們給0,或者未指定,,如果不是一行中的第一元素,并且通過 getUsablewWidth()方法獲取一行中可用的寬度,不夠容納下一元素,時就新創(chuàng)建一個集合,來裝一行中所有元素,再把所有的子View元素全部測量完成后,我們還需要通過setMeasuredDemoetion()方法把測量出來的寬和高保存起來,保存之后可以調(diào)用getMeasureWidth獲取測量之后的寬了.
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { allLines.clear(); //測量容器的寬和高 int containerMeasuredWidth = MeasureSpec.getSize(widthMeasureSpec); //這個集合用于保存單行 ArrayList<View> oneLine = null; for (int i = 0; i < getChildCount(); i++) { //獲取每一Chiledview View child = getChildAt(i); int UnspecifiedMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); child.measure(UnspecifiedMeasureSpec, UnspecifiedMeasureSpec);//相當(dāng)于傳了一個0,0; //如果是第1個view就new一個新行出來,或者View大于了可用的寬度, if (i == 0 || child.getMeasuredWidth() > getUsablewWidth(containerMeasuredWidth, oneLine,oneLine.size())) { oneLine = new ArrayList<View>(); allLines.add(oneLine); } oneLine.add(child); } int lineNumber = allLines.size(); int allLinesHeight = getChildAt(0).getMeasuredHeight() * lineNumber; int verticalTotalpadding = getPaddingBottom() + getPaddingTop(); //垂直總的spcing int verticalTotalSpcing = 8 * (lineNumber - 1); //容器的高 = 所有View的高 + 垂直方向的Padding + 垂直總的spcing int containerMeasureHeight = allLinesHeight + verticalTotalpadding + verticalTotalSpcing; setMeasuredDimension(containerMeasuredWidth, containerMeasureHeight); }
3.獲取一行中可用的空間
獲取一行中可用的寬度,需要我們傳入容器的寬度,和一行元素的集合,和元素之間的間隔,,然后遍歷所有的元素,通過一個變量來保存所有View測量出來寬度的總和,用容器的寬 減去,子View寬度的總和減去水平方向的間隔,以及左右兩邊的Padding,得到一行中可用的寬度
private int getUsablewWidth(int containerMeasuredWidth, ArrayList<View> oneLine,int needSpacingCount) { int oneLineWidth = 0; for (View view : oneLine) { oneLineWidth += view.getMeasuredWidth(); } //水平方向兩邊的padding int horizotalPadding = getPaddingLeft() + getPaddingRight(); int horizontalTotalSpcing = horizotalPadding * needSpacingCount; int usablewWidth = containerMeasuredWidth - oneLineWidth - horizotalPadding - horizontalTotalSpcing; return usablewWidth; }
4.對所有的子View進(jìn)行排版
還是遍歷每一行中的每一個元素,對該元素執(zhí)行排版方法,通過child.getMeasuredWidth();和child.getMeasuredHeight();獲取測量后的View的寬和高,通過child.layout(l,t,r,b),對View進(jìn)行位置的擺放,left就是上個元素的Rigth,Top,就是上一行元素的Bootom,Rigth就是Left+View自身的寬度,Bottom是Top+View自身的高度,最后,因?yàn)槲覀兪謩影裈extView的寬改變了,跟測量時的寬不一樣了,重新調(diào)用測量即可
protected void onLayout(boolean changed, int l, int t, int r, int b) { int tempRight = 0;//保存一行中上一個View的Right int tempBottom = 0;//保存上一行View的Bottom位置 ///遍歷第一排 for (int row = 0; row < allLines.size(); row++) { ArrayList<View> oneLines = allLines.get(row); //計算一行中每個Veiw可以分到的平均寬度 int totalUsableWidth= getUsablewWidth(getMeasuredWidth(), oneLines,oneLines.size()-1); int averageUsablewWidth = totalUsableWidth/oneLines.size(); //遍歷的是一行的內(nèi)容 for (int column = 0; column < oneLines.size(); column++) { View child = oneLines.get(column); //獲取測量的寬高 int measuredWidth = child.getMeasuredWidth(); int measuredHeight = child.getMeasuredHeight(); //如果是一行中的第一個View則排在第0個位置 int left = column == 0 ? getPaddingLeft() : tempRight + 8; //如果是第1行Top坐標(biāo)是PaddingTop的位置,否則就上一個View的bottom位置 int top = row == 0 ? getPaddingTop() : tempBottom + 8; int right = left + measuredWidth ;//+ averageUsablewWidth; int bootom = top + measuredHeight; child.layout(left, top, right, bootom); tempRight = right; int WidthMeasureSpec = MeasureSpec.makeMeasureSpec(child.getWidth(), MeasureSpec.EXACTLY); int HeightMakeMeasureSpec = MeasureSpec.makeMeasureSpec(child.getHeight(), MeasureSpec.EXACTLY); child.measure(WidthMeasureSpec,HeightMakeMeasureSpec); } tempBottom = oneLines.get(0).getBottom(); } }
5.Activity
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); FlowLayout flowLayout = new FlowLayout(this); flowLayout.setPadding(6, 6, 6, 6); for (String text : list) { TextView textView = new TextView(this); textView.setBackgroundResource(R.drawable.bg_text); textView.setGravity(Gravity.CENTER); textView.setPadding(6, 6, 6, 6); textView.setText(text); textView.setTextSize(20); flowLayout.addView(textView); } setContentView(flowLayout); } }
6.TextView 的背景
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <stroke android:width="1dp" android:color="#5000" /> <corners android:radius="6dp"/> </shape>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android編程之ActionBar Tabs用法實(shí)例分析
這篇文章主要介紹了Android編程之ActionBar Tabs用法,結(jié)合實(shí)例形式分析了ActionBar Tabs的功能及Tab切換不同的Fragment的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-03-03Android開發(fā)自學(xué)筆記(四):APP布局下
這篇文章主要介紹了Android開發(fā)自學(xué)筆記(四):APP布局下,本文是上一篇的補(bǔ)充,需要的朋友可以參考下2015-04-04Android中關(guān)于相對布局RelativeLayout的技巧匯總
RelativeLayout是相對布局控件,以控件之間相對位置或相對父容器位置進(jìn)行排列。下面這篇文章主要給大家介紹了關(guān)于Android中相對布局RelativeLayout的一些技巧,需要的朋友可以參考借鑒,下面來一起看看吧。2017-02-02Android自定義View之簡約風(fēng)歌詞控件實(shí)戰(zhàn)指南
一些接觸Android不久的朋友對自定義View都有一絲畏懼感,總感覺這是一個比較高級的技術(shù),但其實(shí)自定義View并不復(fù)雜,有時候只需要簡單幾行代碼就可以完成了,這篇文章主要給大家介紹了關(guān)于Android自定義View之簡約風(fēng)歌詞控件的相關(guān)資料,需要的朋友可以參考下2021-07-07解決Android自定義view獲取attr中自定義顏色的問題
這篇文章主要介紹了Android自定義view獲取attr中自定義顏色的問題解決方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12flutter實(shí)現(xiàn)appbar下選項(xiàng)卡切換
這篇文章主要為大家詳細(xì)介紹了flutter實(shí)現(xiàn)appbar下選項(xiàng)卡切換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-07-07