Android自定義ViewGroup實現流式布局
本文實例為大家分享了Android自定義ViewGroup實現流式布局的具體代碼,供大家參考,具體內容如下
1.概述
本篇給大家帶來一個實例,FlowLayout,什么是FlowLayout,我們常在App 的搜索界面看到熱門搜索詞,就是FlowLayout,我們要實現的就是圖中的效果,就是根據容器的寬,往容器里面添加元素,如果剩余的控件不足時候,自行添加到下一行,FlowLayout也叫流式布局,在開發中還是挺常用的.
2.對所有的子View進行測量
onMeasure方法的調用次數是不確定的,所以為了避免測量出錯,需要把總的List集合,清空一下,一個View的繪制,需要經過onMeasure方法的測量,和onLayout方法的排版才能顯示出來,在測量的方法中,我們把該ViewGroup中的所有子View遍歷出來,添加到一行中的List集合中,再把一行中的所有的元素集合添加到總的集合中去,并對每個子View元素進行測量,測量的參數,我們給0,或者未指定,,如果不是一行中的第一元素,并且通過 getUsablewWidth()方法獲取一行中可用的寬度,不夠容納下一元素,時就新創建一個集合,來裝一行中所有元素,再把所有的子View元素全部測量完成后,我們還需要通過setMeasuredDemoetion()方法把測量出來的寬和高保存起來,保存之后可以調用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);//相當于傳了一個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進行排版
還是遍歷每一行中的每一個元素,對該元素執行排版方法,通過child.getMeasuredWidth();和child.getMeasuredHeight();獲取測量后的View的寬和高,通過child.layout(l,t,r,b),對View進行位置的擺放,left就是上個元素的Rigth,Top,就是上一行元素的Bootom,Rigth就是Left+View自身的寬度,Bottom是Top+View自身的高度,最后,因為我們手動把TextView的寬改變了,跟測量時的寬不一樣了,重新調用測量即可
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(); //遍歷的是一行的內容 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坐標是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: android:color='#5000' /> <corners android:radius='6dp'/></shape>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章: