本文主要介紹自動(dòng)換行l(wèi)inearlayout,下面一起看看自動(dòng)換行l(wèi)inearlayout相關(guān)資訊。
轉(zhuǎn):(22條消息)radiogroup的博客帶自動(dòng)換行點(diǎn)擊事件_再見美猴王_ -csdn博客。
公共類myradiogroup擴(kuò)展了radio group { private static final string tag = radiogroupex public my radio group(context context){ super(context);} public my radio group(context context,attributeset attrs){ super(context,attrs);} @ override protected void on measure(int widthmeasurespec,int heightsmeasurespec){ int width size = measurespec . getsize(widthmeasurespec);int width mode = measure spec . get mode(widthmeasurespec);int heightsize = measure spec . getsize(heightsmeasurespec);int height mode = measure spec . get mode(heightmaurespec);//調(diào)用viewgroup的方法來度量視圖measure children(width measure spec,heightmeasurespec);//最大寬度int maxwidth = 0;//累計(jì)高度int total height = 0;//當(dāng)前行的累計(jì)線寬int line width = 0;//當(dāng)前行的最大行高int maxline height = 0;//用于記錄換行前的線寬和行高int oldheightint舊寬度;int count = get child count;//假設(shè)widthmode和heightmode都是_ most for(int i = 0;我數(shù);i){ view child = get child at(i);邊緣layout params params =(marginlayoutparams)child . getlayout params;//獲取該行的最高oldheight = maxlineheight//當(dāng)前最大寬度oldwidth = maxwidthint deltax = child . getmeasuredwidthparams . left margin params . right margin;if(line width deltax getpadding lightgetpadding lightwidth size){//如果換行,高度增加//與當(dāng)前最大寬度相比,獲得最寬寬度。你可以 t加上當(dāng)前子的寬度,所以你用寬度maxwidth = math.max (line width,width);//reset width line width = deltax;//累積高度totalheight = oldheight//重置行高。當(dāng)前視圖屬于下一行,因此當(dāng)前最大行高是該子級(jí)的高度加上marginmax line height = child。getmeasureheight參數(shù)。上邊距參數(shù)。底部邊距;log.v(標(biāo)簽, maxheightcho 0-@ . com 總高度 - maxwidth: maxwidth);} else {//不換行,累計(jì)寬度linewidth = deltax//不換行,計(jì)算最高int deltay = child . getmeasureheightparams . top margin params . bottom margin;maxline height = math . max(maxline height,deltay);} if (i == count-1) {//在它之前不添加下一行。如果是最后一行,必須疊加最后一行的最高值:totalheight = maxlineheight//將計(jì)算的最后一行與其前面最寬的一行進(jìn)行比較maxwidth = math.max(線寬,舊寬度);} }//加上當(dāng)前容器的填充值maxwidth = getpadding lightgetpadding light;total height = getpadding topgetpadding bottom;setmeasureddimension(width mode = = measure spec。確實(shí)如此。寬度尺寸:最大寬度,高度模式==測(cè)量規(guī)格。確實(shí)如此。h:總高度);} @ override protected void onlayout(boolean changed,int l,int t,int r,int b){ int count = get child count;//pre是所有前一個(gè)子元素的添加位置。int pre left = getpadding left;int pretop = getpadding top;//記錄每行的最高值int max height = 0;for(int i = 0;我數(shù);i){ view child = get child at(i);marginlayoutparams params =(marginlayoutparams)child . getlayoutparams;//r-l是當(dāng)前容器的寬度。如果子視圖的累積寬度大于容器寬度,則換行。if(前置左參數(shù)。左邊距子對(duì)象。getmeasurewidth參數(shù)。右邊距getpadding light(r-l)){//reset pre left = getpadding left;//選擇身高最大的孩子作為設(shè)置pretop = pretop maxheightmaxheight = getchildat(i)。getmeasuredheightparams . top margin params . bottom margin;} else {//不換行,計(jì)算最大高度max height = math.max (max height,child。getmeasureheight參數(shù)。上邊距參數(shù)。下邊距);}//左坐標(biāo)intleft = prelefparams。左邊距;//top coordinate int top = pretop params . top margin;int right = left child . getmeasuredwidth;int bottom = top child . getmeasuredheight;//子視圖的layout child.layout (left,top,right,bottom);//計(jì)算布局后,preleft = params的值。leftmarginchild。getmeasurewidth參數(shù)。右邊距;}}}標(biāo)簽:
寬度包裝
了解更多自動(dòng)換行l(wèi)inearlayout相關(guān)內(nèi)容請(qǐng)關(guān)注本站點(diǎn)。