国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

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

瀏覽:2日期:2022-09-22 14:28:21

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

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

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: android:color='#5000' /> <corners android:radius='6dp'/></shape>

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

標(biāo)簽: Android
相關(guān)文章:
主站蜘蛛池模板: 欧美怡红院免费的视频 | 国产精品不卡无毒在线观看 | 午夜大片免费男女爽爽影院久久 | 日韩一级片在线免费观看 | 精品三级国产一区二区三区四区 | 成人久久在线 | 99久久精品久久久 | 成人午夜在线观看国产 | 一色屋精品亚洲香蕉网站 | 免费一级肉体全黄毛片 | 欧美一级在线视频 | 成人性视频在线三级 | 免费观看三级毛片 | 亚洲精品三区 | 欧美成人a级在线视频 | 找个毛片看看 | 亚洲一级视频在线观看 | 成视频年人黄网站免费 | 久久久www成人免费精品 | 毛片久久 | 成人黄色毛片 | 亚洲精品高清视频 | 亚洲视频在线观看视频 | 国产亚洲国产bv网站在线 | 美女张开腿给男人捅 | 亚洲午夜一区二区三区 | 亚欧美| 理论片中文字幕 | 亚洲视频偷拍自拍 | 亚洲一成人毛片 | 韩日一级 | 国产精品分类视频分类一区 | 精品在线观看免费 | 永久天堂| 欧美一级aa毛片禁片 | 亚洲一区中文字幕在线 | 国产理论视频 | 久久香焦 | 99亚洲自拍| 久久综合色88 | 91探花福利精品国产自产在线 |