Android custom ViewGroup for streaming layouts

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.