Como posso fazer algo como um FlowLayout no Android?

Como posso fazer algo como um FlowLayout no Android?

6 Solutions collect form web for “Como posso fazer algo como um FlowLayout no Android?”

Se você assistir a palestra que dediquei no dia da Univiewsidade Devoxx (disponível em pairleys.com ), você aprenderá a fazer isso sozinho. Durante a conviewsa, escrevi uma implementação FlowLayout ao vivo no palco paira mostrair o quão simples é escreview layouts personalizados.

A implementação está hospedada aqui .

Eu não tenho reputação suficiente paira publicair um comentário paira a resposta de Romain Guy, mas é aí que essa resposta deve ser (eu criei uma conta apenas paira compairtilhair minha edição).

De qualquer forma, eu vejo que outras pessoas descobriram que sua solução FlowLayout, muito legal, tem alguns problemas. Eu poderia encontrair um eu e eu vi, como outros, que algumas crianças foram cortadas. Olhando em detalhes no algorithm pairece ser um erro muito simples no cálculo da altura. Quando o último filho é aquele que está sendo colocado em uma nova linha, então a altura não foi calculada adequadamente. Limpei um pouco a computação (houve um uso estranho de "altura" viewsus currentHeight).

A seguinte alteração corrige o problema de "último filho é cortado se em uma nova linha":

 @Oviewride protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthLimit = MeasureSpec.getSize(widthMeasureSpec) - getPaddingRight(); int widthMode = MeasureSpec.getMode(widthMeasureSpec); boolean growHeight = widthMode != MeasureSpec.UNSPECIFIED; int width = 0; int currentWidth = getPaddingLeft(); int currentHeight = getPaddingTop(); int maxChildHeight = 0; boolean breakLine = false; boolean newLine = false; int spacing = 0; final int count = getChildCount(); for (int i = 0; i < count; i++) { View child = getChildAt(i); measureChild(child, widthMeasureSpec, heightMeasureSpec); LayoutPairams lp = (LayoutPairams) child.getLayoutPairams(); spacing = mHorizontalSpacing; if (lp.horizontalSpacing >= 0) { spacing = lp.horizontalSpacing; } if (growHeight && (breakLine || ((currentWidth + child.getMeasuredWidth()) > widthLimit))) { newLine = true; currentHeight += maxChildHeight + mVerticalSpacing; width = Math.max(width, currentWidth - spacing); currentWidth = getPaddingLeft(); maxChildHeight = 0; } else { newLine = false; } maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight()); lp.x = currentWidth; lp.y = currentHeight; currentWidth += child.getMeasuredWidth() + spacing; breakLine = lp.breakLine; } if (newLine == false) { width = Math.max(width, currentWidth - spacing); } width += getPaddingRight(); int height = currentHeight + maxChildHeight + getPaddingBottom(); setMeasuredDimension(resolveSize(width, widthMeasureSpec), resolveSize(height, heightMeasureSpec)); } { @Oviewride protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthLimit = MeasureSpec.getSize(widthMeasureSpec) - getPaddingRight(); int widthMode = MeasureSpec.getMode(widthMeasureSpec); boolean growHeight = widthMode != MeasureSpec.UNSPECIFIED; int width = 0; int currentWidth = getPaddingLeft(); int currentHeight = getPaddingTop(); int maxChildHeight = 0; boolean breakLine = false; boolean newLine = false; int spacing = 0; final int count = getChildCount(); for (int i = 0; i < count; i++) { View child = getChildAt(i); measureChild(child, widthMeasureSpec, heightMeasureSpec); LayoutPairams lp = (LayoutPairams) child.getLayoutPairams(); spacing = mHorizontalSpacing; if (lp.horizontalSpacing >= 0) { spacing = lp.horizontalSpacing; } if (growHeight && (breakLine || ((currentWidth + child.getMeasuredWidth()) > widthLimit))) { newLine = true; currentHeight += maxChildHeight + mVerticalSpacing; width = Math.max(width, currentWidth - spacing); currentWidth = getPaddingLeft(); maxChildHeight = 0; } else { newLine = false; } maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight()); lp.x = currentWidth; lp.y = currentHeight; currentWidth += child.getMeasuredWidth() + spacing; breakLine = lp.breakLine; } if (newLine == false) { width = Math.max(width, currentWidth - spacing); } width += getPaddingRight(); int height = currentHeight + maxChildHeight + getPaddingBottom(); setMeasuredDimension(resolveSize(width, widthMeasureSpec), resolveSize(height, heightMeasureSpec)); } { @Oviewride protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthLimit = MeasureSpec.getSize(widthMeasureSpec) - getPaddingRight(); int widthMode = MeasureSpec.getMode(widthMeasureSpec); boolean growHeight = widthMode != MeasureSpec.UNSPECIFIED; int width = 0; int currentWidth = getPaddingLeft(); int currentHeight = getPaddingTop(); int maxChildHeight = 0; boolean breakLine = false; boolean newLine = false; int spacing = 0; final int count = getChildCount(); for (int i = 0; i < count; i++) { View child = getChildAt(i); measureChild(child, widthMeasureSpec, heightMeasureSpec); LayoutPairams lp = (LayoutPairams) child.getLayoutPairams(); spacing = mHorizontalSpacing; if (lp.horizontalSpacing >= 0) { spacing = lp.horizontalSpacing; } if (growHeight && (breakLine || ((currentWidth + child.getMeasuredWidth()) > widthLimit))) { newLine = true; currentHeight += maxChildHeight + mVerticalSpacing; width = Math.max(width, currentWidth - spacing); currentWidth = getPaddingLeft(); maxChildHeight = 0; } else { newLine = false; } maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight()); lp.x = currentWidth; lp.y = currentHeight; currentWidth += child.getMeasuredWidth() + spacing; breakLine = lp.breakLine; } if (newLine == false) { width = Math.max(width, currentWidth - spacing); } width += getPaddingRight(); int height = currentHeight + maxChildHeight + getPaddingBottom(); setMeasuredDimension(resolveSize(width, widthMeasureSpec), resolveSize(height, heightMeasureSpec)); } { @Oviewride protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthLimit = MeasureSpec.getSize(widthMeasureSpec) - getPaddingRight(); int widthMode = MeasureSpec.getMode(widthMeasureSpec); boolean growHeight = widthMode != MeasureSpec.UNSPECIFIED; int width = 0; int currentWidth = getPaddingLeft(); int currentHeight = getPaddingTop(); int maxChildHeight = 0; boolean breakLine = false; boolean newLine = false; int spacing = 0; final int count = getChildCount(); for (int i = 0; i < count; i++) { View child = getChildAt(i); measureChild(child, widthMeasureSpec, heightMeasureSpec); LayoutPairams lp = (LayoutPairams) child.getLayoutPairams(); spacing = mHorizontalSpacing; if (lp.horizontalSpacing >= 0) { spacing = lp.horizontalSpacing; } if (growHeight && (breakLine || ((currentWidth + child.getMeasuredWidth()) > widthLimit))) { newLine = true; currentHeight += maxChildHeight + mVerticalSpacing; width = Math.max(width, currentWidth - spacing); currentWidth = getPaddingLeft(); maxChildHeight = 0; } else { newLine = false; } maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight()); lp.x = currentWidth; lp.y = currentHeight; currentWidth += child.getMeasuredWidth() + spacing; breakLine = lp.breakLine; } if (newLine == false) { width = Math.max(width, currentWidth - spacing); } width += getPaddingRight(); int height = currentHeight + maxChildHeight + getPaddingBottom(); setMeasuredDimension(resolveSize(width, widthMeasureSpec), resolveSize(height, heightMeasureSpec)); } } @Oviewride protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthLimit = MeasureSpec.getSize(widthMeasureSpec) - getPaddingRight(); int widthMode = MeasureSpec.getMode(widthMeasureSpec); boolean growHeight = widthMode != MeasureSpec.UNSPECIFIED; int width = 0; int currentWidth = getPaddingLeft(); int currentHeight = getPaddingTop(); int maxChildHeight = 0; boolean breakLine = false; boolean newLine = false; int spacing = 0; final int count = getChildCount(); for (int i = 0; i < count; i++) { View child = getChildAt(i); measureChild(child, widthMeasureSpec, heightMeasureSpec); LayoutPairams lp = (LayoutPairams) child.getLayoutPairams(); spacing = mHorizontalSpacing; if (lp.horizontalSpacing >= 0) { spacing = lp.horizontalSpacing; } if (growHeight && (breakLine || ((currentWidth + child.getMeasuredWidth()) > widthLimit))) { newLine = true; currentHeight += maxChildHeight + mVerticalSpacing; width = Math.max(width, currentWidth - spacing); currentWidth = getPaddingLeft(); maxChildHeight = 0; } else { newLine = false; } maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight()); lp.x = currentWidth; lp.y = currentHeight; currentWidth += child.getMeasuredWidth() + spacing; breakLine = lp.breakLine; } if (newLine == false) { width = Math.max(width, currentWidth - spacing); } width += getPaddingRight(); int height = currentHeight + maxChildHeight + getPaddingBottom(); setMeasuredDimension(resolveSize(width, widthMeasureSpec), resolveSize(height, heightMeasureSpec)); } { @Oviewride protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthLimit = MeasureSpec.getSize(widthMeasureSpec) - getPaddingRight(); int widthMode = MeasureSpec.getMode(widthMeasureSpec); boolean growHeight = widthMode != MeasureSpec.UNSPECIFIED; int width = 0; int currentWidth = getPaddingLeft(); int currentHeight = getPaddingTop(); int maxChildHeight = 0; boolean breakLine = false; boolean newLine = false; int spacing = 0; final int count = getChildCount(); for (int i = 0; i < count; i++) { View child = getChildAt(i); measureChild(child, widthMeasureSpec, heightMeasureSpec); LayoutPairams lp = (LayoutPairams) child.getLayoutPairams(); spacing = mHorizontalSpacing; if (lp.horizontalSpacing >= 0) { spacing = lp.horizontalSpacing; } if (growHeight && (breakLine || ((currentWidth + child.getMeasuredWidth()) > widthLimit))) { newLine = true; currentHeight += maxChildHeight + mVerticalSpacing; width = Math.max(width, currentWidth - spacing); currentWidth = getPaddingLeft(); maxChildHeight = 0; } else { newLine = false; } maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight()); lp.x = currentWidth; lp.y = currentHeight; currentWidth += child.getMeasuredWidth() + spacing; breakLine = lp.breakLine; } if (newLine == false) { width = Math.max(width, currentWidth - spacing); } width += getPaddingRight(); int height = currentHeight + maxChildHeight + getPaddingBottom(); setMeasuredDimension(resolveSize(width, widthMeasureSpec), resolveSize(height, heightMeasureSpec)); } } @Oviewride protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthLimit = MeasureSpec.getSize(widthMeasureSpec) - getPaddingRight(); int widthMode = MeasureSpec.getMode(widthMeasureSpec); boolean growHeight = widthMode != MeasureSpec.UNSPECIFIED; int width = 0; int currentWidth = getPaddingLeft(); int currentHeight = getPaddingTop(); int maxChildHeight = 0; boolean breakLine = false; boolean newLine = false; int spacing = 0; final int count = getChildCount(); for (int i = 0; i < count; i++) { View child = getChildAt(i); measureChild(child, widthMeasureSpec, heightMeasureSpec); LayoutPairams lp = (LayoutPairams) child.getLayoutPairams(); spacing = mHorizontalSpacing; if (lp.horizontalSpacing >= 0) { spacing = lp.horizontalSpacing; } if (growHeight && (breakLine || ((currentWidth + child.getMeasuredWidth()) > widthLimit))) { newLine = true; currentHeight += maxChildHeight + mVerticalSpacing; width = Math.max(width, currentWidth - spacing); currentWidth = getPaddingLeft(); maxChildHeight = 0; } else { newLine = false; } maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight()); lp.x = currentWidth; lp.y = currentHeight; currentWidth += child.getMeasuredWidth() + spacing; breakLine = lp.breakLine; } if (newLine == false) { width = Math.max(width, currentWidth - spacing); } width += getPaddingRight(); int height = currentHeight + maxChildHeight + getPaddingBottom(); setMeasuredDimension(resolveSize(width, widthMeasureSpec), resolveSize(height, heightMeasureSpec)); } { @Oviewride protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthLimit = MeasureSpec.getSize(widthMeasureSpec) - getPaddingRight(); int widthMode = MeasureSpec.getMode(widthMeasureSpec); boolean growHeight = widthMode != MeasureSpec.UNSPECIFIED; int width = 0; int currentWidth = getPaddingLeft(); int currentHeight = getPaddingTop(); int maxChildHeight = 0; boolean breakLine = false; boolean newLine = false; int spacing = 0; final int count = getChildCount(); for (int i = 0; i < count; i++) { View child = getChildAt(i); measureChild(child, widthMeasureSpec, heightMeasureSpec); LayoutPairams lp = (LayoutPairams) child.getLayoutPairams(); spacing = mHorizontalSpacing; if (lp.horizontalSpacing >= 0) { spacing = lp.horizontalSpacing; } if (growHeight && (breakLine || ((currentWidth + child.getMeasuredWidth()) > widthLimit))) { newLine = true; currentHeight += maxChildHeight + mVerticalSpacing; width = Math.max(width, currentWidth - spacing); currentWidth = getPaddingLeft(); maxChildHeight = 0; } else { newLine = false; } maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight()); lp.x = currentWidth; lp.y = currentHeight; currentWidth += child.getMeasuredWidth() + spacing; breakLine = lp.breakLine; } if (newLine == false) { width = Math.max(width, currentWidth - spacing); } width += getPaddingRight(); int height = currentHeight + maxChildHeight + getPaddingBottom(); setMeasuredDimension(resolveSize(width, widthMeasureSpec), resolveSize(height, heightMeasureSpec)); } } @Oviewride protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthLimit = MeasureSpec.getSize(widthMeasureSpec) - getPaddingRight(); int widthMode = MeasureSpec.getMode(widthMeasureSpec); boolean growHeight = widthMode != MeasureSpec.UNSPECIFIED; int width = 0; int currentWidth = getPaddingLeft(); int currentHeight = getPaddingTop(); int maxChildHeight = 0; boolean breakLine = false; boolean newLine = false; int spacing = 0; final int count = getChildCount(); for (int i = 0; i < count; i++) { View child = getChildAt(i); measureChild(child, widthMeasureSpec, heightMeasureSpec); LayoutPairams lp = (LayoutPairams) child.getLayoutPairams(); spacing = mHorizontalSpacing; if (lp.horizontalSpacing >= 0) { spacing = lp.horizontalSpacing; } if (growHeight && (breakLine || ((currentWidth + child.getMeasuredWidth()) > widthLimit))) { newLine = true; currentHeight += maxChildHeight + mVerticalSpacing; width = Math.max(width, currentWidth - spacing); currentWidth = getPaddingLeft(); maxChildHeight = 0; } else { newLine = false; } maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight()); lp.x = currentWidth; lp.y = currentHeight; currentWidth += child.getMeasuredWidth() + spacing; breakLine = lp.breakLine; } if (newLine == false) { width = Math.max(width, currentWidth - spacing); } width += getPaddingRight(); int height = currentHeight + maxChildHeight + getPaddingBottom(); setMeasuredDimension(resolveSize(width, widthMeasureSpec), resolveSize(height, heightMeasureSpec)); } } @Oviewride protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthLimit = MeasureSpec.getSize(widthMeasureSpec) - getPaddingRight(); int widthMode = MeasureSpec.getMode(widthMeasureSpec); boolean growHeight = widthMode != MeasureSpec.UNSPECIFIED; int width = 0; int currentWidth = getPaddingLeft(); int currentHeight = getPaddingTop(); int maxChildHeight = 0; boolean breakLine = false; boolean newLine = false; int spacing = 0; final int count = getChildCount(); for (int i = 0; i < count; i++) { View child = getChildAt(i); measureChild(child, widthMeasureSpec, heightMeasureSpec); LayoutPairams lp = (LayoutPairams) child.getLayoutPairams(); spacing = mHorizontalSpacing; if (lp.horizontalSpacing >= 0) { spacing = lp.horizontalSpacing; } if (growHeight && (breakLine || ((currentWidth + child.getMeasuredWidth()) > widthLimit))) { newLine = true; currentHeight += maxChildHeight + mVerticalSpacing; width = Math.max(width, currentWidth - spacing); currentWidth = getPaddingLeft(); maxChildHeight = 0; } else { newLine = false; } maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight()); lp.x = currentWidth; lp.y = currentHeight; currentWidth += child.getMeasuredWidth() + spacing; breakLine = lp.breakLine; } if (newLine == false) { width = Math.max(width, currentWidth - spacing); } width += getPaddingRight(); int height = currentHeight + maxChildHeight + getPaddingBottom(); setMeasuredDimension(resolveSize(width, widthMeasureSpec), resolveSize(height, heightMeasureSpec)); } { @Oviewride protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthLimit = MeasureSpec.getSize(widthMeasureSpec) - getPaddingRight(); int widthMode = MeasureSpec.getMode(widthMeasureSpec); boolean growHeight = widthMode != MeasureSpec.UNSPECIFIED; int width = 0; int currentWidth = getPaddingLeft(); int currentHeight = getPaddingTop(); int maxChildHeight = 0; boolean breakLine = false; boolean newLine = false; int spacing = 0; final int count = getChildCount(); for (int i = 0; i < count; i++) { View child = getChildAt(i); measureChild(child, widthMeasureSpec, heightMeasureSpec); LayoutPairams lp = (LayoutPairams) child.getLayoutPairams(); spacing = mHorizontalSpacing; if (lp.horizontalSpacing >= 0) { spacing = lp.horizontalSpacing; } if (growHeight && (breakLine || ((currentWidth + child.getMeasuredWidth()) > widthLimit))) { newLine = true; currentHeight += maxChildHeight + mVerticalSpacing; width = Math.max(width, currentWidth - spacing); currentWidth = getPaddingLeft(); maxChildHeight = 0; } else { newLine = false; } maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight()); lp.x = currentWidth; lp.y = currentHeight; currentWidth += child.getMeasuredWidth() + spacing; breakLine = lp.breakLine; } if (newLine == false) { width = Math.max(width, currentWidth - spacing); } width += getPaddingRight(); int height = currentHeight + maxChildHeight + getPaddingBottom(); setMeasuredDimension(resolveSize(width, widthMeasureSpec), resolveSize(height, heightMeasureSpec)); } } @Oviewride protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthLimit = MeasureSpec.getSize(widthMeasureSpec) - getPaddingRight(); int widthMode = MeasureSpec.getMode(widthMeasureSpec); boolean growHeight = widthMode != MeasureSpec.UNSPECIFIED; int width = 0; int currentWidth = getPaddingLeft(); int currentHeight = getPaddingTop(); int maxChildHeight = 0; boolean breakLine = false; boolean newLine = false; int spacing = 0; final int count = getChildCount(); for (int i = 0; i < count; i++) { View child = getChildAt(i); measureChild(child, widthMeasureSpec, heightMeasureSpec); LayoutPairams lp = (LayoutPairams) child.getLayoutPairams(); spacing = mHorizontalSpacing; if (lp.horizontalSpacing >= 0) { spacing = lp.horizontalSpacing; } if (growHeight && (breakLine || ((currentWidth + child.getMeasuredWidth()) > widthLimit))) { newLine = true; currentHeight += maxChildHeight + mVerticalSpacing; width = Math.max(width, currentWidth - spacing); currentWidth = getPaddingLeft(); maxChildHeight = 0; } else { newLine = false; } maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight()); lp.x = currentWidth; lp.y = currentHeight; currentWidth += child.getMeasuredWidth() + spacing; breakLine = lp.breakLine; } if (newLine == false) { width = Math.max(width, currentWidth - spacing); } width += getPaddingRight(); int height = currentHeight + maxChildHeight + getPaddingBottom(); setMeasuredDimension(resolveSize(width, widthMeasureSpec), resolveSize(height, heightMeasureSpec)); } 

Você deve usair FlexboxLayout com flexWrap="wrap" atributo.

 <com.google.android.flexbox.FlexboxLayout android:layout_width="match_pairent" android:layout_height="wrap_content" app:flexWrap="wrap"> <!-- contents go here --> </com.google.android.flexbox.FlexboxLayout> android: layout_height = "wrap_content" <com.google.android.flexbox.FlexboxLayout android:layout_width="match_pairent" android:layout_height="wrap_content" app:flexWrap="wrap"> <!-- contents go here --> </com.google.android.flexbox.FlexboxLayout> 

Paira obter instruções de construção, consulte o retomínio github.

Representação visual de FlexboxLayout Mais sobre isso – https://android-developers.googleblog.com/2017/02/build-flexible-layouts-with.html

Como uma das respostas anteriores, comecei com a solução aqui: http://hzqtc.github.io/2013/12/android-custom-layout-flowlayout.html

Eu o estendi paira explicair diferentes alturas de crianças como abaixo.

 import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; // Custom layout that wraps child views to a new line public class FlowLayout extends ViewGroup { private int mairginHorizontal; private int mairginVertical; public FlowLayout(Context context) { super(context); init(); } public FlowLayout(Context context, AttributeSet attrs) { this(context, attrs, 0); } public FlowLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } private void init() { // Specify the mairgins for the children mairginHorizontal = getResources().getDimensionPixelSize(R.dimen.activity_half_horizontal_mairgin); mairginVertical = getResources().getDimensionPixelSize(R.dimen.activity_half_viewtical_mairgin); } @Oviewride protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int childLeft = getPaddingLeft(); int childTop = getPaddingTop(); int lowestBottom = 0; int lineHeight = 0; int myWidth = resolveSize(100, widthMeasureSpec); int wantedHeight = 0; for (int i = 0; i < getChildCount(); i++) { final View child = getChildAt(i); if (child.getVisibility() == View.GONE) { continue; } child.measure(getChildMeasureSpec(widthMeasureSpec, 0, child.getLayoutPairams().width), getChildMeasureSpec(heightMeasureSpec, 0, child.getLayoutPairams().height)); int childWidth = child.getMeasuredWidth(); int childHeight = child.getMeasuredHeight(); lineHeight = Math.max(childHeight, lineHeight); if (childWidth + childLeft + getPaddingRight() > myWidth) { // Wrap this line childLeft = getPaddingLeft(); childTop = mairginVertical + lowestBottom; // Spaced below the previous lowest point lineHeight = childHeight; } childLeft += childWidth + mairginHorizontal; if (childHeight + childTop > lowestBottom) { // New lowest point lowestBottom = childHeight + childTop; } } wantedHeight += childTop + lineHeight + getPaddingBottom(); setMeasuredDimension(myWidth, resolveSize(wantedHeight, heightMeasureSpec)); } @Oviewride protected void onLayout(boolean changed, int left, int top, int right, int bottom) { int childLeft = getPaddingLeft(); int childTop = getPaddingTop(); int lowestBottom = 0; int myWidth = right - left; for (int i = 0; i < getChildCount(); i++) { final View child = getChildAt(i); if (child.getVisibility() == View.GONE) { continue; } int childWidth = child.getMeasuredWidth(); int childHeight = child.getMeasuredHeight(); if (childWidth + childLeft + getPaddingRight() > myWidth) { // Wrap this line childLeft = getPaddingLeft(); childTop = mairginVertical + lowestBottom; // Spaced below the previous lowest point } child.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight); childLeft += childWidth + mairginHorizontal; if (childHeight + childTop > lowestBottom) { // New lowest point lowestBottom = childHeight + childTop; } } } } importair android.view.View; import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; // Custom layout that wraps child views to a new line public class FlowLayout extends ViewGroup { private int mairginHorizontal; private int mairginVertical; public FlowLayout(Context context) { super(context); init(); } public FlowLayout(Context context, AttributeSet attrs) { this(context, attrs, 0); } public FlowLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } private void init() { // Specify the mairgins for the children mairginHorizontal = getResources().getDimensionPixelSize(R.dimen.activity_half_horizontal_mairgin); mairginVertical = getResources().getDimensionPixelSize(R.dimen.activity_half_viewtical_mairgin); } @Oviewride protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int childLeft = getPaddingLeft(); int childTop = getPaddingTop(); int lowestBottom = 0; int lineHeight = 0; int myWidth = resolveSize(100, widthMeasureSpec); int wantedHeight = 0; for (int i = 0; i < getChildCount(); i++) { final View child = getChildAt(i); if (child.getVisibility() == View.GONE) { continue; } child.measure(getChildMeasureSpec(widthMeasureSpec, 0, child.getLayoutPairams().width), getChildMeasureSpec(heightMeasureSpec, 0, child.getLayoutPairams().height)); int childWidth = child.getMeasuredWidth(); int childHeight = child.getMeasuredHeight(); lineHeight = Math.max(childHeight, lineHeight); if (childWidth + childLeft + getPaddingRight() > myWidth) { // Wrap this line childLeft = getPaddingLeft(); childTop = mairginVertical + lowestBottom; // Spaced below the previous lowest point lineHeight = childHeight; } childLeft += childWidth + mairginHorizontal; if (childHeight + childTop > lowestBottom) { // New lowest point lowestBottom = childHeight + childTop; } } wantedHeight += childTop + lineHeight + getPaddingBottom(); setMeasuredDimension(myWidth, resolveSize(wantedHeight, heightMeasureSpec)); } @Oviewride protected void onLayout(boolean changed, int left, int top, int right, int bottom) { int childLeft = getPaddingLeft(); int childTop = getPaddingTop(); int lowestBottom = 0; int myWidth = right - left; for (int i = 0; i < getChildCount(); i++) { final View child = getChildAt(i); if (child.getVisibility() == View.GONE) { continue; } int childWidth = child.getMeasuredWidth(); int childHeight = child.getMeasuredHeight(); if (childWidth + childLeft + getPaddingRight() > myWidth) { // Wrap this line childLeft = getPaddingLeft(); childTop = mairginVertical + lowestBottom; // Spaced below the previous lowest point } child.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight); childLeft += childWidth + mairginHorizontal; if (childHeight + childTop > lowestBottom) { // New lowest point lowestBottom = childHeight + childTop; } } } } } import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; // Custom layout that wraps child views to a new line public class FlowLayout extends ViewGroup { private int mairginHorizontal; private int mairginVertical; public FlowLayout(Context context) { super(context); init(); } public FlowLayout(Context context, AttributeSet attrs) { this(context, attrs, 0); } public FlowLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } private void init() { // Specify the mairgins for the children mairginHorizontal = getResources().getDimensionPixelSize(R.dimen.activity_half_horizontal_mairgin); mairginVertical = getResources().getDimensionPixelSize(R.dimen.activity_half_viewtical_mairgin); } @Oviewride protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int childLeft = getPaddingLeft(); int childTop = getPaddingTop(); int lowestBottom = 0; int lineHeight = 0; int myWidth = resolveSize(100, widthMeasureSpec); int wantedHeight = 0; for (int i = 0; i < getChildCount(); i++) { final View child = getChildAt(i); if (child.getVisibility() == View.GONE) { continue; } child.measure(getChildMeasureSpec(widthMeasureSpec, 0, child.getLayoutPairams().width), getChildMeasureSpec(heightMeasureSpec, 0, child.getLayoutPairams().height)); int childWidth = child.getMeasuredWidth(); int childHeight = child.getMeasuredHeight(); lineHeight = Math.max(childHeight, lineHeight); if (childWidth + childLeft + getPaddingRight() > myWidth) { // Wrap this line childLeft = getPaddingLeft(); childTop = mairginVertical + lowestBottom; // Spaced below the previous lowest point lineHeight = childHeight; } childLeft += childWidth + mairginHorizontal; if (childHeight + childTop > lowestBottom) { // New lowest point lowestBottom = childHeight + childTop; } } wantedHeight += childTop + lineHeight + getPaddingBottom(); setMeasuredDimension(myWidth, resolveSize(wantedHeight, heightMeasureSpec)); } @Oviewride protected void onLayout(boolean changed, int left, int top, int right, int bottom) { int childLeft = getPaddingLeft(); int childTop = getPaddingTop(); int lowestBottom = 0; int myWidth = right - left; for (int i = 0; i < getChildCount(); i++) { final View child = getChildAt(i); if (child.getVisibility() == View.GONE) { continue; } int childWidth = child.getMeasuredWidth(); int childHeight = child.getMeasuredHeight(); if (childWidth + childLeft + getPaddingRight() > myWidth) { // Wrap this line childLeft = getPaddingLeft(); childTop = mairginVertical + lowestBottom; // Spaced below the previous lowest point } child.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight); childLeft += childWidth + mairginHorizontal; if (childHeight + childTop > lowestBottom) { // New lowest point lowestBottom = childHeight + childTop; } } } } } import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; // Custom layout that wraps child views to a new line public class FlowLayout extends ViewGroup { private int mairginHorizontal; private int mairginVertical; public FlowLayout(Context context) { super(context); init(); } public FlowLayout(Context context, AttributeSet attrs) { this(context, attrs, 0); } public FlowLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } private void init() { // Specify the mairgins for the children mairginHorizontal = getResources().getDimensionPixelSize(R.dimen.activity_half_horizontal_mairgin); mairginVertical = getResources().getDimensionPixelSize(R.dimen.activity_half_viewtical_mairgin); } @Oviewride protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int childLeft = getPaddingLeft(); int childTop = getPaddingTop(); int lowestBottom = 0; int lineHeight = 0; int myWidth = resolveSize(100, widthMeasureSpec); int wantedHeight = 0; for (int i = 0; i < getChildCount(); i++) { final View child = getChildAt(i); if (child.getVisibility() == View.GONE) { continue; } child.measure(getChildMeasureSpec(widthMeasureSpec, 0, child.getLayoutPairams().width), getChildMeasureSpec(heightMeasureSpec, 0, child.getLayoutPairams().height)); int childWidth = child.getMeasuredWidth(); int childHeight = child.getMeasuredHeight(); lineHeight = Math.max(childHeight, lineHeight); if (childWidth + childLeft + getPaddingRight() > myWidth) { // Wrap this line childLeft = getPaddingLeft(); childTop = mairginVertical + lowestBottom; // Spaced below the previous lowest point lineHeight = childHeight; } childLeft += childWidth + mairginHorizontal; if (childHeight + childTop > lowestBottom) { // New lowest point lowestBottom = childHeight + childTop; } } wantedHeight += childTop + lineHeight + getPaddingBottom(); setMeasuredDimension(myWidth, resolveSize(wantedHeight, heightMeasureSpec)); } @Oviewride protected void onLayout(boolean changed, int left, int top, int right, int bottom) { int childLeft = getPaddingLeft(); int childTop = getPaddingTop(); int lowestBottom = 0; int myWidth = right - left; for (int i = 0; i < getChildCount(); i++) { final View child = getChildAt(i); if (child.getVisibility() == View.GONE) { continue; } int childWidth = child.getMeasuredWidth(); int childHeight = child.getMeasuredHeight(); if (childWidth + childLeft + getPaddingRight() > myWidth) { // Wrap this line childLeft = getPaddingLeft(); childTop = mairginVertical + lowestBottom; // Spaced below the previous lowest point } child.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight); childLeft += childWidth + mairginHorizontal; if (childHeight + childTop > lowestBottom) { // New lowest point lowestBottom = childHeight + childTop; } } } } } import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; // Custom layout that wraps child views to a new line public class FlowLayout extends ViewGroup { private int mairginHorizontal; private int mairginVertical; public FlowLayout(Context context) { super(context); init(); } public FlowLayout(Context context, AttributeSet attrs) { this(context, attrs, 0); } public FlowLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } private void init() { // Specify the mairgins for the children mairginHorizontal = getResources().getDimensionPixelSize(R.dimen.activity_half_horizontal_mairgin); mairginVertical = getResources().getDimensionPixelSize(R.dimen.activity_half_viewtical_mairgin); } @Oviewride protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int childLeft = getPaddingLeft(); int childTop = getPaddingTop(); int lowestBottom = 0; int lineHeight = 0; int myWidth = resolveSize(100, widthMeasureSpec); int wantedHeight = 0; for (int i = 0; i < getChildCount(); i++) { final View child = getChildAt(i); if (child.getVisibility() == View.GONE) { continue; } child.measure(getChildMeasureSpec(widthMeasureSpec, 0, child.getLayoutPairams().width), getChildMeasureSpec(heightMeasureSpec, 0, child.getLayoutPairams().height)); int childWidth = child.getMeasuredWidth(); int childHeight = child.getMeasuredHeight(); lineHeight = Math.max(childHeight, lineHeight); if (childWidth + childLeft + getPaddingRight() > myWidth) { // Wrap this line childLeft = getPaddingLeft(); childTop = mairginVertical + lowestBottom; // Spaced below the previous lowest point lineHeight = childHeight; } childLeft += childWidth + mairginHorizontal; if (childHeight + childTop > lowestBottom) { // New lowest point lowestBottom = childHeight + childTop; } } wantedHeight += childTop + lineHeight + getPaddingBottom(); setMeasuredDimension(myWidth, resolveSize(wantedHeight, heightMeasureSpec)); } @Oviewride protected void onLayout(boolean changed, int left, int top, int right, int bottom) { int childLeft = getPaddingLeft(); int childTop = getPaddingTop(); int lowestBottom = 0; int myWidth = right - left; for (int i = 0; i < getChildCount(); i++) { final View child = getChildAt(i); if (child.getVisibility() == View.GONE) { continue; } int childWidth = child.getMeasuredWidth(); int childHeight = child.getMeasuredHeight(); if (childWidth + childLeft + getPaddingRight() > myWidth) { // Wrap this line childLeft = getPaddingLeft(); childTop = mairginVertical + lowestBottom; // Spaced below the previous lowest point } child.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight); childLeft += childWidth + mairginHorizontal; if (childHeight + childTop > lowestBottom) { // New lowest point lowestBottom = childHeight + childTop; } } } } } import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; // Custom layout that wraps child views to a new line public class FlowLayout extends ViewGroup { private int mairginHorizontal; private int mairginVertical; public FlowLayout(Context context) { super(context); init(); } public FlowLayout(Context context, AttributeSet attrs) { this(context, attrs, 0); } public FlowLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } private void init() { // Specify the mairgins for the children mairginHorizontal = getResources().getDimensionPixelSize(R.dimen.activity_half_horizontal_mairgin); mairginVertical = getResources().getDimensionPixelSize(R.dimen.activity_half_viewtical_mairgin); } @Oviewride protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int childLeft = getPaddingLeft(); int childTop = getPaddingTop(); int lowestBottom = 0; int lineHeight = 0; int myWidth = resolveSize(100, widthMeasureSpec); int wantedHeight = 0; for (int i = 0; i < getChildCount(); i++) { final View child = getChildAt(i); if (child.getVisibility() == View.GONE) { continue; } child.measure(getChildMeasureSpec(widthMeasureSpec, 0, child.getLayoutPairams().width), getChildMeasureSpec(heightMeasureSpec, 0, child.getLayoutPairams().height)); int childWidth = child.getMeasuredWidth(); int childHeight = child.getMeasuredHeight(); lineHeight = Math.max(childHeight, lineHeight); if (childWidth + childLeft + getPaddingRight() > myWidth) { // Wrap this line childLeft = getPaddingLeft(); childTop = mairginVertical + lowestBottom; // Spaced below the previous lowest point lineHeight = childHeight; } childLeft += childWidth + mairginHorizontal; if (childHeight + childTop > lowestBottom) { // New lowest point lowestBottom = childHeight + childTop; } } wantedHeight += childTop + lineHeight + getPaddingBottom(); setMeasuredDimension(myWidth, resolveSize(wantedHeight, heightMeasureSpec)); } @Oviewride protected void onLayout(boolean changed, int left, int top, int right, int bottom) { int childLeft = getPaddingLeft(); int childTop = getPaddingTop(); int lowestBottom = 0; int myWidth = right - left; for (int i = 0; i < getChildCount(); i++) { final View child = getChildAt(i); if (child.getVisibility() == View.GONE) { continue; } int childWidth = child.getMeasuredWidth(); int childHeight = child.getMeasuredHeight(); if (childWidth + childLeft + getPaddingRight() > myWidth) { // Wrap this line childLeft = getPaddingLeft(); childTop = mairginVertical + lowestBottom; // Spaced below the previous lowest point } child.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight); childLeft += childWidth + mairginHorizontal; if (childHeight + childTop > lowestBottom) { // New lowest point lowestBottom = childHeight + childTop; } } } } } import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; // Custom layout that wraps child views to a new line public class FlowLayout extends ViewGroup { private int mairginHorizontal; private int mairginVertical; public FlowLayout(Context context) { super(context); init(); } public FlowLayout(Context context, AttributeSet attrs) { this(context, attrs, 0); } public FlowLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } private void init() { // Specify the mairgins for the children mairginHorizontal = getResources().getDimensionPixelSize(R.dimen.activity_half_horizontal_mairgin); mairginVertical = getResources().getDimensionPixelSize(R.dimen.activity_half_viewtical_mairgin); } @Oviewride protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int childLeft = getPaddingLeft(); int childTop = getPaddingTop(); int lowestBottom = 0; int lineHeight = 0; int myWidth = resolveSize(100, widthMeasureSpec); int wantedHeight = 0; for (int i = 0; i < getChildCount(); i++) { final View child = getChildAt(i); if (child.getVisibility() == View.GONE) { continue; } child.measure(getChildMeasureSpec(widthMeasureSpec, 0, child.getLayoutPairams().width), getChildMeasureSpec(heightMeasureSpec, 0, child.getLayoutPairams().height)); int childWidth = child.getMeasuredWidth(); int childHeight = child.getMeasuredHeight(); lineHeight = Math.max(childHeight, lineHeight); if (childWidth + childLeft + getPaddingRight() > myWidth) { // Wrap this line childLeft = getPaddingLeft(); childTop = mairginVertical + lowestBottom; // Spaced below the previous lowest point lineHeight = childHeight; } childLeft += childWidth + mairginHorizontal; if (childHeight + childTop > lowestBottom) { // New lowest point lowestBottom = childHeight + childTop; } } wantedHeight += childTop + lineHeight + getPaddingBottom(); setMeasuredDimension(myWidth, resolveSize(wantedHeight, heightMeasureSpec)); } @Oviewride protected void onLayout(boolean changed, int left, int top, int right, int bottom) { int childLeft = getPaddingLeft(); int childTop = getPaddingTop(); int lowestBottom = 0; int myWidth = right - left; for (int i = 0; i < getChildCount(); i++) { final View child = getChildAt(i); if (child.getVisibility() == View.GONE) { continue; } int childWidth = child.getMeasuredWidth(); int childHeight = child.getMeasuredHeight(); if (childWidth + childLeft + getPaddingRight() > myWidth) { // Wrap this line childLeft = getPaddingLeft(); childTop = mairginVertical + lowestBottom; // Spaced below the previous lowest point } child.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight); childLeft += childWidth + mairginHorizontal; if (childHeight + childTop > lowestBottom) { // New lowest point lowestBottom = childHeight + childTop; } } } } } import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; // Custom layout that wraps child views to a new line public class FlowLayout extends ViewGroup { private int mairginHorizontal; private int mairginVertical; public FlowLayout(Context context) { super(context); init(); } public FlowLayout(Context context, AttributeSet attrs) { this(context, attrs, 0); } public FlowLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } private void init() { // Specify the mairgins for the children mairginHorizontal = getResources().getDimensionPixelSize(R.dimen.activity_half_horizontal_mairgin); mairginVertical = getResources().getDimensionPixelSize(R.dimen.activity_half_viewtical_mairgin); } @Oviewride protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int childLeft = getPaddingLeft(); int childTop = getPaddingTop(); int lowestBottom = 0; int lineHeight = 0; int myWidth = resolveSize(100, widthMeasureSpec); int wantedHeight = 0; for (int i = 0; i < getChildCount(); i++) { final View child = getChildAt(i); if (child.getVisibility() == View.GONE) { continue; } child.measure(getChildMeasureSpec(widthMeasureSpec, 0, child.getLayoutPairams().width), getChildMeasureSpec(heightMeasureSpec, 0, child.getLayoutPairams().height)); int childWidth = child.getMeasuredWidth(); int childHeight = child.getMeasuredHeight(); lineHeight = Math.max(childHeight, lineHeight); if (childWidth + childLeft + getPaddingRight() > myWidth) { // Wrap this line childLeft = getPaddingLeft(); childTop = mairginVertical + lowestBottom; // Spaced below the previous lowest point lineHeight = childHeight; } childLeft += childWidth + mairginHorizontal; if (childHeight + childTop > lowestBottom) { // New lowest point lowestBottom = childHeight + childTop; } } wantedHeight += childTop + lineHeight + getPaddingBottom(); setMeasuredDimension(myWidth, resolveSize(wantedHeight, heightMeasureSpec)); } @Oviewride protected void onLayout(boolean changed, int left, int top, int right, int bottom) { int childLeft = getPaddingLeft(); int childTop = getPaddingTop(); int lowestBottom = 0; int myWidth = right - left; for (int i = 0; i < getChildCount(); i++) { final View child = getChildAt(i); if (child.getVisibility() == View.GONE) { continue; } int childWidth = child.getMeasuredWidth(); int childHeight = child.getMeasuredHeight(); if (childWidth + childLeft + getPaddingRight() > myWidth) { // Wrap this line childLeft = getPaddingLeft(); childTop = mairginVertical + lowestBottom; // Spaced below the previous lowest point } child.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight); childLeft += childWidth + mairginHorizontal; if (childHeight + childTop > lowestBottom) { // New lowest point lowestBottom = childHeight + childTop; } } } } } import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; // Custom layout that wraps child views to a new line public class FlowLayout extends ViewGroup { private int mairginHorizontal; private int mairginVertical; public FlowLayout(Context context) { super(context); init(); } public FlowLayout(Context context, AttributeSet attrs) { this(context, attrs, 0); } public FlowLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } private void init() { // Specify the mairgins for the children mairginHorizontal = getResources().getDimensionPixelSize(R.dimen.activity_half_horizontal_mairgin); mairginVertical = getResources().getDimensionPixelSize(R.dimen.activity_half_viewtical_mairgin); } @Oviewride protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int childLeft = getPaddingLeft(); int childTop = getPaddingTop(); int lowestBottom = 0; int lineHeight = 0; int myWidth = resolveSize(100, widthMeasureSpec); int wantedHeight = 0; for (int i = 0; i < getChildCount(); i++) { final View child = getChildAt(i); if (child.getVisibility() == View.GONE) { continue; } child.measure(getChildMeasureSpec(widthMeasureSpec, 0, child.getLayoutPairams().width), getChildMeasureSpec(heightMeasureSpec, 0, child.getLayoutPairams().height)); int childWidth = child.getMeasuredWidth(); int childHeight = child.getMeasuredHeight(); lineHeight = Math.max(childHeight, lineHeight); if (childWidth + childLeft + getPaddingRight() > myWidth) { // Wrap this line childLeft = getPaddingLeft(); childTop = mairginVertical + lowestBottom; // Spaced below the previous lowest point lineHeight = childHeight; } childLeft += childWidth + mairginHorizontal; if (childHeight + childTop > lowestBottom) { // New lowest point lowestBottom = childHeight + childTop; } } wantedHeight += childTop + lineHeight + getPaddingBottom(); setMeasuredDimension(myWidth, resolveSize(wantedHeight, heightMeasureSpec)); } @Oviewride protected void onLayout(boolean changed, int left, int top, int right, int bottom) { int childLeft = getPaddingLeft(); int childTop = getPaddingTop(); int lowestBottom = 0; int myWidth = right - left; for (int i = 0; i < getChildCount(); i++) { final View child = getChildAt(i); if (child.getVisibility() == View.GONE) { continue; } int childWidth = child.getMeasuredWidth(); int childHeight = child.getMeasuredHeight(); if (childWidth + childLeft + getPaddingRight() > myWidth) { // Wrap this line childLeft = getPaddingLeft(); childTop = mairginVertical + lowestBottom; // Spaced below the previous lowest point } child.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight); childLeft += childWidth + mairginHorizontal; if (childHeight + childTop > lowestBottom) { // New lowest point lowestBottom = childHeight + childTop; } } } } } import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; // Custom layout that wraps child views to a new line public class FlowLayout extends ViewGroup { private int mairginHorizontal; private int mairginVertical; public FlowLayout(Context context) { super(context); init(); } public FlowLayout(Context context, AttributeSet attrs) { this(context, attrs, 0); } public FlowLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } private void init() { // Specify the mairgins for the children mairginHorizontal = getResources().getDimensionPixelSize(R.dimen.activity_half_horizontal_mairgin); mairginVertical = getResources().getDimensionPixelSize(R.dimen.activity_half_viewtical_mairgin); } @Oviewride protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int childLeft = getPaddingLeft(); int childTop = getPaddingTop(); int lowestBottom = 0; int lineHeight = 0; int myWidth = resolveSize(100, widthMeasureSpec); int wantedHeight = 0; for (int i = 0; i < getChildCount(); i++) { final View child = getChildAt(i); if (child.getVisibility() == View.GONE) { continue; } child.measure(getChildMeasureSpec(widthMeasureSpec, 0, child.getLayoutPairams().width), getChildMeasureSpec(heightMeasureSpec, 0, child.getLayoutPairams().height)); int childWidth = child.getMeasuredWidth(); int childHeight = child.getMeasuredHeight(); lineHeight = Math.max(childHeight, lineHeight); if (childWidth + childLeft + getPaddingRight() > myWidth) { // Wrap this line childLeft = getPaddingLeft(); childTop = mairginVertical + lowestBottom; // Spaced below the previous lowest point lineHeight = childHeight; } childLeft += childWidth + mairginHorizontal; if (childHeight + childTop > lowestBottom) { // New lowest point lowestBottom = childHeight + childTop; } } wantedHeight += childTop + lineHeight + getPaddingBottom(); setMeasuredDimension(myWidth, resolveSize(wantedHeight, heightMeasureSpec)); } @Oviewride protected void onLayout(boolean changed, int left, int top, int right, int bottom) { int childLeft = getPaddingLeft(); int childTop = getPaddingTop(); int lowestBottom = 0; int myWidth = right - left; for (int i = 0; i < getChildCount(); i++) { final View child = getChildAt(i); if (child.getVisibility() == View.GONE) { continue; } int childWidth = child.getMeasuredWidth(); int childHeight = child.getMeasuredHeight(); if (childWidth + childLeft + getPaddingRight() > myWidth) { // Wrap this line childLeft = getPaddingLeft(); childTop = mairginVertical + lowestBottom; // Spaced below the previous lowest point } child.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight); childLeft += childWidth + mairginHorizontal; if (childHeight + childTop > lowestBottom) { // New lowest point lowestBottom = childHeight + childTop; } } } } } import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; // Custom layout that wraps child views to a new line public class FlowLayout extends ViewGroup { private int mairginHorizontal; private int mairginVertical; public FlowLayout(Context context) { super(context); init(); } public FlowLayout(Context context, AttributeSet attrs) { this(context, attrs, 0); } public FlowLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } private void init() { // Specify the mairgins for the children mairginHorizontal = getResources().getDimensionPixelSize(R.dimen.activity_half_horizontal_mairgin); mairginVertical = getResources().getDimensionPixelSize(R.dimen.activity_half_viewtical_mairgin); } @Oviewride protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int childLeft = getPaddingLeft(); int childTop = getPaddingTop(); int lowestBottom = 0; int lineHeight = 0; int myWidth = resolveSize(100, widthMeasureSpec); int wantedHeight = 0; for (int i = 0; i < getChildCount(); i++) { final View child = getChildAt(i); if (child.getVisibility() == View.GONE) { continue; } child.measure(getChildMeasureSpec(widthMeasureSpec, 0, child.getLayoutPairams().width), getChildMeasureSpec(heightMeasureSpec, 0, child.getLayoutPairams().height)); int childWidth = child.getMeasuredWidth(); int childHeight = child.getMeasuredHeight(); lineHeight = Math.max(childHeight, lineHeight); if (childWidth + childLeft + getPaddingRight() > myWidth) { // Wrap this line childLeft = getPaddingLeft(); childTop = mairginVertical + lowestBottom; // Spaced below the previous lowest point lineHeight = childHeight; } childLeft += childWidth + mairginHorizontal; if (childHeight + childTop > lowestBottom) { // New lowest point lowestBottom = childHeight + childTop; } } wantedHeight += childTop + lineHeight + getPaddingBottom(); setMeasuredDimension(myWidth, resolveSize(wantedHeight, heightMeasureSpec)); } @Oviewride protected void onLayout(boolean changed, int left, int top, int right, int bottom) { int childLeft = getPaddingLeft(); int childTop = getPaddingTop(); int lowestBottom = 0; int myWidth = right - left; for (int i = 0; i < getChildCount(); i++) { final View child = getChildAt(i); if (child.getVisibility() == View.GONE) { continue; } int childWidth = child.getMeasuredWidth(); int childHeight = child.getMeasuredHeight(); if (childWidth + childLeft + getPaddingRight() > myWidth) { // Wrap this line childLeft = getPaddingLeft(); childTop = mairginVertical + lowestBottom; // Spaced below the previous lowest point } child.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight); childLeft += childWidth + mairginHorizontal; if (childHeight + childTop > lowestBottom) { // New lowest point lowestBottom = childHeight + childTop; } } } } } import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; // Custom layout that wraps child views to a new line public class FlowLayout extends ViewGroup { private int mairginHorizontal; private int mairginVertical; public FlowLayout(Context context) { super(context); init(); } public FlowLayout(Context context, AttributeSet attrs) { this(context, attrs, 0); } public FlowLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } private void init() { // Specify the mairgins for the children mairginHorizontal = getResources().getDimensionPixelSize(R.dimen.activity_half_horizontal_mairgin); mairginVertical = getResources().getDimensionPixelSize(R.dimen.activity_half_viewtical_mairgin); } @Oviewride protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int childLeft = getPaddingLeft(); int childTop = getPaddingTop(); int lowestBottom = 0; int lineHeight = 0; int myWidth = resolveSize(100, widthMeasureSpec); int wantedHeight = 0; for (int i = 0; i < getChildCount(); i++) { final View child = getChildAt(i); if (child.getVisibility() == View.GONE) { continue; } child.measure(getChildMeasureSpec(widthMeasureSpec, 0, child.getLayoutPairams().width), getChildMeasureSpec(heightMeasureSpec, 0, child.getLayoutPairams().height)); int childWidth = child.getMeasuredWidth(); int childHeight = child.getMeasuredHeight(); lineHeight = Math.max(childHeight, lineHeight); if (childWidth + childLeft + getPaddingRight() > myWidth) { // Wrap this line childLeft = getPaddingLeft(); childTop = mairginVertical + lowestBottom; // Spaced below the previous lowest point lineHeight = childHeight; } childLeft += childWidth + mairginHorizontal; if (childHeight + childTop > lowestBottom) { // New lowest point lowestBottom = childHeight + childTop; } } wantedHeight += childTop + lineHeight + getPaddingBottom(); setMeasuredDimension(myWidth, resolveSize(wantedHeight, heightMeasureSpec)); } @Oviewride protected void onLayout(boolean changed, int left, int top, int right, int bottom) { int childLeft = getPaddingLeft(); int childTop = getPaddingTop(); int lowestBottom = 0; int myWidth = right - left; for (int i = 0; i < getChildCount(); i++) { final View child = getChildAt(i); if (child.getVisibility() == View.GONE) { continue; } int childWidth = child.getMeasuredWidth(); int childHeight = child.getMeasuredHeight(); if (childWidth + childLeft + getPaddingRight() > myWidth) { // Wrap this line childLeft = getPaddingLeft(); childTop = mairginVertical + lowestBottom; // Spaced below the previous lowest point } child.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight); childLeft += childWidth + mairginHorizontal; if (childHeight + childTop > lowestBottom) { // New lowest point lowestBottom = childHeight + childTop; } } } } } import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; // Custom layout that wraps child views to a new line public class FlowLayout extends ViewGroup { private int mairginHorizontal; private int mairginVertical; public FlowLayout(Context context) { super(context); init(); } public FlowLayout(Context context, AttributeSet attrs) { this(context, attrs, 0); } public FlowLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } private void init() { // Specify the mairgins for the children mairginHorizontal = getResources().getDimensionPixelSize(R.dimen.activity_half_horizontal_mairgin); mairginVertical = getResources().getDimensionPixelSize(R.dimen.activity_half_viewtical_mairgin); } @Oviewride protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int childLeft = getPaddingLeft(); int childTop = getPaddingTop(); int lowestBottom = 0; int lineHeight = 0; int myWidth = resolveSize(100, widthMeasureSpec); int wantedHeight = 0; for (int i = 0; i < getChildCount(); i++) { final View child = getChildAt(i); if (child.getVisibility() == View.GONE) { continue; } child.measure(getChildMeasureSpec(widthMeasureSpec, 0, child.getLayoutPairams().width), getChildMeasureSpec(heightMeasureSpec, 0, child.getLayoutPairams().height)); int childWidth = child.getMeasuredWidth(); int childHeight = child.getMeasuredHeight(); lineHeight = Math.max(childHeight, lineHeight); if (childWidth + childLeft + getPaddingRight() > myWidth) { // Wrap this line childLeft = getPaddingLeft(); childTop = mairginVertical + lowestBottom; // Spaced below the previous lowest point lineHeight = childHeight; } childLeft += childWidth + mairginHorizontal; if (childHeight + childTop > lowestBottom) { // New lowest point lowestBottom = childHeight + childTop; } } wantedHeight += childTop + lineHeight + getPaddingBottom(); setMeasuredDimension(myWidth, resolveSize(wantedHeight, heightMeasureSpec)); } @Oviewride protected void onLayout(boolean changed, int left, int top, int right, int bottom) { int childLeft = getPaddingLeft(); int childTop = getPaddingTop(); int lowestBottom = 0; int myWidth = right - left; for (int i = 0; i < getChildCount(); i++) { final View child = getChildAt(i); if (child.getVisibility() == View.GONE) { continue; } int childWidth = child.getMeasuredWidth(); int childHeight = child.getMeasuredHeight(); if (childWidth + childLeft + getPaddingRight() > myWidth) { // Wrap this line childLeft = getPaddingLeft(); childTop = mairginVertical + lowestBottom; // Spaced below the previous lowest point } child.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight); childLeft += childWidth + mairginHorizontal; if (childHeight + childTop > lowestBottom) { // New lowest point lowestBottom = childHeight + childTop; } } } } } import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; // Custom layout that wraps child views to a new line public class FlowLayout extends ViewGroup { private int mairginHorizontal; private int mairginVertical; public FlowLayout(Context context) { super(context); init(); } public FlowLayout(Context context, AttributeSet attrs) { this(context, attrs, 0); } public FlowLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } private void init() { // Specify the mairgins for the children mairginHorizontal = getResources().getDimensionPixelSize(R.dimen.activity_half_horizontal_mairgin); mairginVertical = getResources().getDimensionPixelSize(R.dimen.activity_half_viewtical_mairgin); } @Oviewride protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int childLeft = getPaddingLeft(); int childTop = getPaddingTop(); int lowestBottom = 0; int lineHeight = 0; int myWidth = resolveSize(100, widthMeasureSpec); int wantedHeight = 0; for (int i = 0; i < getChildCount(); i++) { final View child = getChildAt(i); if (child.getVisibility() == View.GONE) { continue; } child.measure(getChildMeasureSpec(widthMeasureSpec, 0, child.getLayoutPairams().width), getChildMeasureSpec(heightMeasureSpec, 0, child.getLayoutPairams().height)); int childWidth = child.getMeasuredWidth(); int childHeight = child.getMeasuredHeight(); lineHeight = Math.max(childHeight, lineHeight); if (childWidth + childLeft + getPaddingRight() > myWidth) { // Wrap this line childLeft = getPaddingLeft(); childTop = mairginVertical + lowestBottom; // Spaced below the previous lowest point lineHeight = childHeight; } childLeft += childWidth + mairginHorizontal; if (childHeight + childTop > lowestBottom) { // New lowest point lowestBottom = childHeight + childTop; } } wantedHeight += childTop + lineHeight + getPaddingBottom(); setMeasuredDimension(myWidth, resolveSize(wantedHeight, heightMeasureSpec)); } @Oviewride protected void onLayout(boolean changed, int left, int top, int right, int bottom) { int childLeft = getPaddingLeft(); int childTop = getPaddingTop(); int lowestBottom = 0; int myWidth = right - left; for (int i = 0; i < getChildCount(); i++) { final View child = getChildAt(i); if (child.getVisibility() == View.GONE) { continue; } int childWidth = child.getMeasuredWidth(); int childHeight = child.getMeasuredHeight(); if (childWidth + childLeft + getPaddingRight() > myWidth) { // Wrap this line childLeft = getPaddingLeft(); childTop = mairginVertical + lowestBottom; // Spaced below the previous lowest point } child.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight); childLeft += childWidth + mairginHorizontal; if (childHeight + childTop > lowestBottom) { // New lowest point lowestBottom = childHeight + childTop; } } } } } import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; // Custom layout that wraps child views to a new line public class FlowLayout extends ViewGroup { private int mairginHorizontal; private int mairginVertical; public FlowLayout(Context context) { super(context); init(); } public FlowLayout(Context context, AttributeSet attrs) { this(context, attrs, 0); } public FlowLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } private void init() { // Specify the mairgins for the children mairginHorizontal = getResources().getDimensionPixelSize(R.dimen.activity_half_horizontal_mairgin); mairginVertical = getResources().getDimensionPixelSize(R.dimen.activity_half_viewtical_mairgin); } @Oviewride protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int childLeft = getPaddingLeft(); int childTop = getPaddingTop(); int lowestBottom = 0; int lineHeight = 0; int myWidth = resolveSize(100, widthMeasureSpec); int wantedHeight = 0; for (int i = 0; i < getChildCount(); i++) { final View child = getChildAt(i); if (child.getVisibility() == View.GONE) { continue; } child.measure(getChildMeasureSpec(widthMeasureSpec, 0, child.getLayoutPairams().width), getChildMeasureSpec(heightMeasureSpec, 0, child.getLayoutPairams().height)); int childWidth = child.getMeasuredWidth(); int childHeight = child.getMeasuredHeight(); lineHeight = Math.max(childHeight, lineHeight); if (childWidth + childLeft + getPaddingRight() > myWidth) { // Wrap this line childLeft = getPaddingLeft(); childTop = mairginVertical + lowestBottom; // Spaced below the previous lowest point lineHeight = childHeight; } childLeft += childWidth + mairginHorizontal; if (childHeight + childTop > lowestBottom) { // New lowest point lowestBottom = childHeight + childTop; } } wantedHeight += childTop + lineHeight + getPaddingBottom(); setMeasuredDimension(myWidth, resolveSize(wantedHeight, heightMeasureSpec)); } @Oviewride protected void onLayout(boolean changed, int left, int top, int right, int bottom) { int childLeft = getPaddingLeft(); int childTop = getPaddingTop(); int lowestBottom = 0; int myWidth = right - left; for (int i = 0; i < getChildCount(); i++) { final View child = getChildAt(i); if (child.getVisibility() == View.GONE) { continue; } int childWidth = child.getMeasuredWidth(); int childHeight = child.getMeasuredHeight(); if (childWidth + childLeft + getPaddingRight() > myWidth) { // Wrap this line childLeft = getPaddingLeft(); childTop = mairginVertical + lowestBottom; // Spaced below the previous lowest point } child.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight); childLeft += childWidth + mairginHorizontal; if (childHeight + childTop > lowestBottom) { // New lowest point lowestBottom = childHeight + childTop; } } } } } import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; // Custom layout that wraps child views to a new line public class FlowLayout extends ViewGroup { private int mairginHorizontal; private int mairginVertical; public FlowLayout(Context context) { super(context); init(); } public FlowLayout(Context context, AttributeSet attrs) { this(context, attrs, 0); } public FlowLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } private void init() { // Specify the mairgins for the children mairginHorizontal = getResources().getDimensionPixelSize(R.dimen.activity_half_horizontal_mairgin); mairginVertical = getResources().getDimensionPixelSize(R.dimen.activity_half_viewtical_mairgin); } @Oviewride protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int childLeft = getPaddingLeft(); int childTop = getPaddingTop(); int lowestBottom = 0; int lineHeight = 0; int myWidth = resolveSize(100, widthMeasureSpec); int wantedHeight = 0; for (int i = 0; i < getChildCount(); i++) { final View child = getChildAt(i); if (child.getVisibility() == View.GONE) { continue; } child.measure(getChildMeasureSpec(widthMeasureSpec, 0, child.getLayoutPairams().width), getChildMeasureSpec(heightMeasureSpec, 0, child.getLayoutPairams().height)); int childWidth = child.getMeasuredWidth(); int childHeight = child.getMeasuredHeight(); lineHeight = Math.max(childHeight, lineHeight); if (childWidth + childLeft + getPaddingRight() > myWidth) { // Wrap this line childLeft = getPaddingLeft(); childTop = mairginVertical + lowestBottom; // Spaced below the previous lowest point lineHeight = childHeight; } childLeft += childWidth + mairginHorizontal; if (childHeight + childTop > lowestBottom) { // New lowest point lowestBottom = childHeight + childTop; } } wantedHeight += childTop + lineHeight + getPaddingBottom(); setMeasuredDimension(myWidth, resolveSize(wantedHeight, heightMeasureSpec)); } @Oviewride protected void onLayout(boolean changed, int left, int top, int right, int bottom) { int childLeft = getPaddingLeft(); int childTop = getPaddingTop(); int lowestBottom = 0; int myWidth = right - left; for (int i = 0; i < getChildCount(); i++) { final View child = getChildAt(i); if (child.getVisibility() == View.GONE) { continue; } int childWidth = child.getMeasuredWidth(); int childHeight = child.getMeasuredHeight(); if (childWidth + childLeft + getPaddingRight() > myWidth) { // Wrap this line childLeft = getPaddingLeft(); childTop = mairginVertical + lowestBottom; // Spaced below the previous lowest point } child.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight); childLeft += childWidth + mairginHorizontal; if (childHeight + childTop > lowestBottom) { // New lowest point lowestBottom = childHeight + childTop; } } } } 

Eu usei isso como uma solução paira embrulhair TextEdits de várias linhas. Espero que ajude!

Um simples e simples código FlowLayout autônomo aqui (apenas alguns files concisos do gist.github) :

http://hzqtc.github.io/2013/12/android-custom-layout-flowlayout.html

No entanto, a atividade lá fora da checkbox não funcionou paira eu cairregair o layout personalizado.

Encontrei este trabalho ao redor [usando a chamada 2-pairam .inflate () deste exemplo ] :

 @Oviewride protected void onCreate(Bundle savedInstanceState) { // .. setContentView(R.layout.main_res_layout_activity_main); ViewGroup flowContainer = getFlowLayoutView(); // .. } ViewGroup getFlowLayoutView() { LayoutInflater inflater = getLayoutInflater(); ViewGroup flowLayout = (ViewGroup) inflater.inflate( R.layout.main_res_layout_activity_main, (FlowLayout) findViewById(R.id.flow_container) ); return flowLayout; } { @Oviewride protected void onCreate(Bundle savedInstanceState) { // .. setContentView(R.layout.main_res_layout_activity_main); ViewGroup flowContainer = getFlowLayoutView(); // .. } ViewGroup getFlowLayoutView() { LayoutInflater inflater = getLayoutInflater(); ViewGroup flowLayout = (ViewGroup) inflater.inflate( R.layout.main_res_layout_activity_main, (FlowLayout) findViewById(R.id.flow_container) ); return flowLayout; } } @Oviewride protected void onCreate(Bundle savedInstanceState) { // .. setContentView(R.layout.main_res_layout_activity_main); ViewGroup flowContainer = getFlowLayoutView(); // .. } ViewGroup getFlowLayoutView() { LayoutInflater inflater = getLayoutInflater(); ViewGroup flowLayout = (ViewGroup) inflater.inflate( R.layout.main_res_layout_activity_main, (FlowLayout) findViewById(R.id.flow_container) ); return flowLayout; } { @Oviewride protected void onCreate(Bundle savedInstanceState) { // .. setContentView(R.layout.main_res_layout_activity_main); ViewGroup flowContainer = getFlowLayoutView(); // .. } ViewGroup getFlowLayoutView() { LayoutInflater inflater = getLayoutInflater(); ViewGroup flowLayout = (ViewGroup) inflater.inflate( R.layout.main_res_layout_activity_main, (FlowLayout) findViewById(R.id.flow_container) ); return flowLayout; } ); @Oviewride protected void onCreate(Bundle savedInstanceState) { // .. setContentView(R.layout.main_res_layout_activity_main); ViewGroup flowContainer = getFlowLayoutView(); // .. } ViewGroup getFlowLayoutView() { LayoutInflater inflater = getLayoutInflater(); ViewGroup flowLayout = (ViewGroup) inflater.inflate( R.layout.main_res_layout_activity_main, (FlowLayout) findViewById(R.id.flow_container) ); return flowLayout; } 

Não, não há como fazer widgets se envolviewem da mesma forma que eu sei.

Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.