国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android自定義ViewGroup之FlowLayout(三)

2019-12-12 05:23:36
字體:
來源:轉載
供稿:網友

本篇繼續來講自定義ViewGroup,給大家帶來一個實例:FlowLayout。何為FlowLayout,就是控件根據ViewGroup的寬,自動的往右添加,如果當前行剩余空間不足,則自動添加到下一行,所以也叫流式布局。Android并沒有提供流式布局,但是某些場合中,流式布局還是非常適合使用的,比如關鍵字標簽,搜索熱詞列表等,比如下圖:


定義FlowLayout

LayoutParams,onLayout的寫法都和上一篇講WaterfallLayout一模一樣,在此不再贅述了,沒看過的可以參照上一篇Android自定義ViewGroup(二)之WaterfallLayout
在這里主要說的是onMeasure方法,注釋見下方:

 @Override  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  super.onMeasure(widthMeasureSpec, heightMeasureSpec);  // 獲得它的父容器為它設置的測量模式和大小  int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);  int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);  int modeWidth = MeasureSpec.getMode(widthMeasureSpec);  int modeHeight = MeasureSpec.getMode(heightMeasureSpec);  int childCount = getChildCount(); // 如果是wrap_content情況下,記錄寬和高  int wrapWidth = 0;  int wrapHeight = 0;  //記錄每一行的寬度,width不斷取最大寬度  int lineWidth = 0;  //每一行的高度,累加至height  int lineHeight = 0; // 遍歷每個子元素  for (int i = 0; i < childCount; i++) {  View child = getChildAt(i);  // 測量每一個child的寬和高  measureChild(child, widthMeasureSpec, heightMeasureSpec);  // 得到child的lParams  LayoutParams lParams = (LayoutParams) child.getLayoutParams();  // 當前子空間實際占據的寬度  int childWidth = child.getMeasuredWidth();  // 當前子空間實際占據的高度  int childHeight = child.getMeasuredHeight();  // 如果加上當前child,則超出最大寬度,然后開啟新行  if (lineWidth + childWidth > sizeWidth) { //記錄新行頭一個標簽坐標,為onLayout做準備 lParams.left = 0; lParams.top = wrapHeight + lineHeight + vSpace; lParams.right = childWidth; lParams.bottom = lParams.top + childHeight; //取最大的,注意這里lineWidth是包括右側hSpace的,需要減掉  wrapWidth = Math.max(lineWidth - hSpace, childWidth);  // 重新開啟新行,開始記錄,可以看到行寬包括最右側hSpace  lineWidth = childWidth + hSpace; // 疊加當前高度,同理,加上下側vSpace wrapHeight += lineHeight + vSpace; // 開啟記錄下一行的高度  lineHeight = childHeight;  } else { //記錄每一個標簽坐標,為onLayout做準備 lParams.left = lineWidth; lParams.top = wrapHeight; lParams.right = lParams.left + childWidth; lParams.bottom = lParams.top + childHeight; //在本行追加標簽,累加值到lineWidth,lineHeight取最大高度  lineWidth += childWidth + hSpace; lineHeight = Math.max(lineHeight, childHeight); }  // 如果是最后一個 if (i == childCount - 1) { //將當前記錄的最大寬度和當前lineWidth做比較,取較大值 wrapWidth = Math.max(wrapWidth, lineWidth - hSpace); //布局高加上最后一行高 wrapHeight += lineHeight;  }  }  setMeasuredDimension((modeWidth == MeasureSpec.EXACTLY) ? sizeWidth : wrapWidth, (modeHeight == MeasureSpec.EXACTLY) ? sizeHeight : wrapHeight);  }

使用FlowLayout

直接看xml吧,一看便知:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:attr="http://schemas.android.com/apk/res/com.hx.flowlayout" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#E1E6F6" android:orientation="vertical" > <com.hx.flowlayout.FlowLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" attr:hSpace="20" attr:vSpace="10"> <TextView style="@style/flow_text_style_1" android:text="標簽" /> <TextView style="@style/flow_text_style_1" android:text="Welcome" /> <TextView style="@style/flow_text_style_1" android:text="IT工程師" /> <TextView style="@style/flow_text_style_1" android:text="程序猿" /> <TextView style="@style/flow_text_style_1" android:text="Android" /> <TextView style="@style/flow_text_style_1" android:text="Java" /> <TextView style="@style/flow_text_style_1" android:text="ViewGroup" /> <TextView style="@style/flow_text_style_1" android:text="FlowLayout" /> </com.hx.flowlayout.FlowLayout> <com.hx.flowlayout.FlowLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" attr:hSpace="20" attr:vSpace="10"> <TextView style="@style/flow_text_style_2" android:text="標簽" /> <TextView style="@style/flow_text_style_2" android:text="Welcome" /> <TextView style="@style/flow_text_style_2" android:text="IT工程師" /> <TextView style="@style/flow_text_style_2" android:text="程序猿" /> <TextView style="@style/flow_text_style_2" android:text="Android" /> <TextView style="@style/flow_text_style_2" android:text="Java" /> <TextView style="@style/flow_text_style_2" android:text="ViewGroup" /> <TextView style="@style/flow_text_style_2" android:text="FlowLayout" /> </com.hx.flowlayout.FlowLayout></LinearLayout>

這里寫的比較

主站蜘蛛池模板: 兖州市| 仁布县| 西吉县| 天柱县| 大理市| 枣庄市| 长阳| 泸水县| 昌都县| 新闻| 两当县| 河曲县| 信丰县| 襄垣县| 武城县| 蓝山县| 溧阳市| 蕲春县| 丹东市| 葵青区| 陆川县| 石台县| 崇左市| 台江县| 通城县| 武冈市| 堆龙德庆县| 武宣县| 保康县| 阳朔县| 潮州市| 仁怀市| 娄烦县| 修水县| 调兵山市| 迁西县| 寻乌县| 吴川市| 南岸区| 额敏县| 承德县|