最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android自定義ViewGroup實(shí)現(xiàn)流式布局

 更新時(shí)間:2020年09月23日 07:47:19   作者:yansong_  
這篇文章主要為大家詳細(xì)介紹了Android自定義ViewGroup實(shí)現(xiàn)流式布局,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Android自定義ViewGroup實(shí)現(xiàn)流式布局的具體代碼,供大家參考,具體內(nèi)容如下

1.概述

本篇給大家?guī)硪粋€(gè)實(shí)例,FlowLayout,什么是FlowLayout,我們常在App 的搜索界面看到熱門搜索詞,就是FlowLayout,我們要實(shí)現(xiàn)的就是圖中的效果,就是根據(jù)容器的寬,往容器里面添加元素,如果剩余的控件不足時(shí)候,自行添加到下一行,FlowLayout也叫流式布局,在開發(fā)中還是挺常用的.

2.對(duì)所有的子View進(jìn)行測(cè)量

onMeasure方法的調(diào)用次數(shù)是不確定的,所以為了避免測(cè)量出錯(cuò),需要把總的List集合,清空一下,一個(gè)View的繪制,需要經(jīng)過onMeasure方法的測(cè)量,和onLayout方法的排版才能顯示出來,在測(cè)量的方法中,我們把該ViewGroup中的所有子View遍歷出來,添加到一行中的List集合中,再把一行中的所有的元素集合添加到總的集合中去,并對(duì)每個(gè)子View元素進(jìn)行測(cè)量,測(cè)量的參數(shù),我們給0,或者未指定,,如果不是一行中的第一元素,并且通過 getUsablewWidth()方法獲取一行中可用的寬度,不夠容納下一元素,時(shí)就新創(chuàng)建一個(gè)集合,來裝一行中所有元素,再把所有的子View元素全部測(cè)量完成后,我們還需要通過setMeasuredDemoetion()方法把測(cè)量出來的寬和高保存起來,保存之后可以調(diào)用getMeasureWidth獲取測(cè)量之后的寬了.

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  allLines.clear();
  //測(cè)量容器的寬和高
  int containerMeasuredWidth = MeasureSpec.getSize(widthMeasureSpec);
  //這個(gè)集合用于保存單行
  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)于傳了一個(gè)0,0;
   //如果是第1個(gè)view就new一個(gè)新行出來,或者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.獲取一行中可用的空間

獲取一行中可用的寬度,需要我們傳入容器的寬度,和一行元素的集合,和元素之間的間隔,,然后遍歷所有的元素,通過一個(gè)變量來保存所有View測(cè)量出來寬度的總和,用容器的寬 減去,子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.對(duì)所有的子View進(jìn)行排版

還是遍歷每一行中的每一個(gè)元素,對(duì)該元素執(zhí)行排版方法,通過child.getMeasuredWidth();和child.getMeasuredHeight();獲取測(cè)量后的View的寬和高,通過child.layout(l,t,r,b),對(duì)View進(jìn)行位置的擺放,left就是上個(gè)元素的Rigth,Top,就是上一行元素的Bootom,Rigth就是Left+View自身的寬度,Bottom是Top+View自身的高度,最后,因?yàn)槲覀兪謩?dòng)把TextView的寬改變了,跟測(cè)量時(shí)的寬不一樣了,重新調(diào)用測(cè)量即可

protected void onLayout(boolean changed, int l, int t, int r, int b) {
  int tempRight = 0;//保存一行中上一個(gè)View的Right
  int tempBottom = 0;//保存上一行View的Bottom位置
  ///遍歷第一排
  for (int row = 0; row < allLines.size(); row++) {
   ArrayList<View> oneLines = allLines.get(row);
   //計(jì)算一行中每個(gè)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);
    //獲取測(cè)量的寬高
    int measuredWidth = child.getMeasuredWidth();
    int measuredHeight = child.getMeasuredHeight();
    //如果是一行中的第一個(gè)View則排在第0個(gè)位置
    int left = column == 0 ? getPaddingLeft() : tempRight + 8;
    //如果是第1行Top坐標(biāo)是PaddingTop的位置,否則就上一個(gè)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>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android EditText追加空格、限制字符等方法示例

    Android EditText追加空格、限制字符等方法示例

    這篇文章主要給大家介紹了關(guān)于Android EditText追加空格、限制字符等的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • Android打開WebView黑屏閃爍問題排查

    Android打開WebView黑屏閃爍問題排查

    這篇文章主要介紹了Android打開WebView黑屏閃爍問題排查,文章通過詳細(xì)的代碼示例和圖文介紹WebView黑屏閃爍的問題,感興趣的小伙伴可以跟著小編一起來學(xué)習(xí)
    2023-05-05
  • Flutter Future異步操作詳細(xì)講解

    Flutter Future異步操作詳細(xì)講解

    這篇文章主要介紹了Flutter Future異步操作,future是Future類的對(duì)象,其表示一個(gè)T類型的異步操作結(jié)果。如果異步操作不需要結(jié)果,則future的類型可為Future
    2023-03-03
  • Android通過ExifInterface判斷Camera圖片方向的方法

    Android通過ExifInterface判斷Camera圖片方向的方法

    今天小編就為大家分享一篇關(guān)于Android通過ExifInterface判斷相機(jī)圖片朝向的方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • 功能強(qiáng)大的Android滾動(dòng)控件RecyclerView

    功能強(qiáng)大的Android滾動(dòng)控件RecyclerView

    這篇文章主要為大家詳細(xì)介紹了功能強(qiáng)大的Android滾動(dòng)控件RecyclerView,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • Android編程實(shí)現(xiàn)播放視頻時(shí)切換全屏并隱藏狀態(tài)欄的方法

    Android編程實(shí)現(xiàn)播放視頻時(shí)切換全屏并隱藏狀態(tài)欄的方法

    這篇文章主要介紹了Android編程實(shí)現(xiàn)播放視頻時(shí)切換全屏并隱藏狀態(tài)欄的方法,結(jié)合實(shí)例形式分析了Android視頻播放事件響應(yīng)及相關(guān)屬性設(shè)置操作技巧,需要的朋友可以參考下
    2017-08-08
  • Android開發(fā)微信小程序頁(yè)面的圖文教程

    Android開發(fā)微信小程序頁(yè)面的圖文教程

    這篇文章主要介紹了Android開發(fā)微信小程序頁(yè)面的教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-10-10
  • Android Studio使用教程(二):基本設(shè)置與運(yùn)行

    Android Studio使用教程(二):基本設(shè)置與運(yùn)行

    這篇文章主要介紹了Android Studio使用教程(二):基本設(shè)置與運(yùn)行,本文講解了項(xiàng)目結(jié)構(gòu)、偏好設(shè)置、常用功能介紹、創(chuàng)建模擬器等內(nèi)容,需要的朋友可以參考下
    2015-05-05
  • Android 8.0安裝apk的實(shí)例代碼

    Android 8.0安裝apk的實(shí)例代碼

    本文給大家分享了Android 8.0安裝apk的實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧
    2018-03-03
  • RecyclerView實(shí)現(xiàn)插入和刪除

    RecyclerView實(shí)現(xiàn)插入和刪除

    這篇文章主要為大家詳細(xì)介紹了RecyclerView實(shí)現(xiàn)插入和刪除,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-08-08

最新評(píng)論

唐山市| 监利县| 延安市| 深州市| 乐亭县| 古蔺县| 枣阳市| 玛沁县| 新乡县| 英德市| 柳林县| 平安县| 农安县| 柳江县| 望江县| 毕节市| 沙田区| 蓝山县| 治多县| 武汉市| 文昌市| 基隆市| 同心县| 南涧| 抚松县| 曲阳县| 赫章县| 左云县| 宣化县| 色达县| 通许县| 潜山县| 克拉玛依市| 望都县| 青海省| 新巴尔虎右旗| 固镇县| 察哈| 南澳县| 西昌市| 石棉县|