To implement a streaming layout with less than the width of the wrapped line:
Flowlayout.java
Package Com.jackie.flowlayout;import Android.content.context;import Android.util.attributeset;import Android.view.view;import android.view.viewgroup;/** * Created by Jackie on 8/28/15. */public class FlowLayout extends ViewGroup {public FlowLayout (context context) {This (context, NULL); } public FlowLayout (context context, AttributeSet Attrs) {Super (context, attrs); } @Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {super.onmeasure (widthmeas Urespec, Heightmeasurespec); Measurechildren (Widthmeasurespec, Heightmeasurespec); } @Override protected void OnLayout (Boolean changed, int left, int top, int. right, int bottom) {/** * generally defined as an int top; a top is actually the subscript of the array left: Specifies the x-coordinate top of the upper-right corner of the rectangle box: Specifies the y-coordinate of the upper-left corner of the rectangle: Specify the x-coordinate of the bottom Bottom: Specifies the y-coordinate of the lower-right corner of the rectangle */int width = getwidth (); int height = getheight (); int tw = 0; int th = 0; for (int i = 0; i < Getchildcount (); i++) {View child = Getchildat (i); if (TW + child.getwidth () < width) {} else {tw = 0; th + + child.getmeasuredheight (); Over screen width, line wrap} child.layout (tw, Th, TW + child.getmeasuredwidth (), Th + child.getmeasuredheight ()) ; TW + = Child.getmeasuredwidth (); } }}
activity_main.xml
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:orien tation= "vertical" > <com.jackie.flowlayout.flowlayout android:layout_width= "match_parent" Android:la yout_height= "Match_parent" > <button android:layout_width= "wrap_content" Android:layout_ height= "Wrap_content" android:text= "will Libai"/> <button android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "Li Bai"/> <button android:l Ayout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "you don't see the Yellow River water Day"/> <button android:layout_width= "wrap_content" android:layout_height= "Wrap_content" an Droid:text= "to the sea no longer"/> <button Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "you don't see high Hall Mirror sad white hair "/> <button android:layout_width=" wrap_content "android:layout_height=" Wrap_cont ENT "android:text=" toward such as hair twilight into snow "/> <button android:layout_width=" Wrap_content "a ndroid:layout_height= "Wrap_content" android:text= "Life proud to unfocused"/> <button android:layout_wi Dth= "Wrap_content" android:layout_height= "wrap_content" android:text= "Mo Jin Bottle empty to month"/> <butt On android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "Natural I will be useful"/> <button android:layout_width= "wrap_content" android:layout_height= "Wrap_co Ntent "android:text="/> </com.jackie.flowlayout.FlowLayout></LinearLayout>
As follows:
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android custom ViewGroup for streaming layouts