Android streaming layout, often used in the history of the search box, people tags and other Android functions, for inexperienced students, to write their own flow layout is really a difficult point, or write out of the custom layout of the running efficiency is not high, here is a very good example of my own collation, take out with you to exchange learning.
1. The Java code of the Flowviewgroup class is as follows
Package Com.snippet.worklearn;
Import Android.content.Context;
Import Android.util.AttributeSet;
Import Android.util.Log;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.TextView;
public class Flowviewgroup extends ViewGroup {
Private final static String TAG = "MyViewGroup2";
Private final static int view_margin = 0;
Public Flowviewgroup (Context context) {
Super (context);
}
Public Flowviewgroup (context context, AttributeSet Attrs) {
Super (context, attrs);
}
@Override
protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {
LOG.D (TAG, "widthmeasurespec =" + Widthmeasurespec
+ "Heightmeasurespec" + heightmeasurespec);
for (int index = 0; index < Getchildcount (); index++) {
Final View child = Getchildat (index);
Measure
Child.measure (measurespec.unspecified, measurespec.unspecified);
// }
int measurewidth = Measurewidth (Widthmeasurespec);
int measureheight = Measureheight (Heightmeasurespec);
Measurechildren (Widthmeasurespec, Heightmeasurespec);
Setmeasureddimension (Measurewidth, measureheight);
Super.onmeasure (Widthmeasurespec, Heightmeasurespec);
}
@Override
protected void OnLayout (Boolean arg0, int arg1, int arg2, int arg3, int arg4) {
Final int count = Getchildcount ();
int row = 0;
int left = Arg1;
int top = ARG2;
int right = ARG3;
int bottom = ARG4;
for (int i = 0; i < count; i++) {
Final View child = This.getchildat (i);
String Text = ((TextView) child). GetText (). toString ();
LOG.E (TAG, text+ "onlayout =" + arg0 + "left =" + Arg1 + "top =" + Arg2 + "right =" + Arg3 + "Botom =" + Arg4);
int width = child.getmeasuredwidth ();
int height = child.getmeasuredheight ();
LOG.E ("----1-----", "width=" +width+ ", height=" +height + ", measuredwidth=" + child.getmeasuredwidth () + ", paddingleft = "+child.getpaddingleft ()
+ ", paddingright=" + child.getpaddingright ());
LOG.E ("----22--before calculation---", "left=" +left+ ", right=" + Right + ", top=" +top ");
Left + + width + view_margin;
top = row * (height + view_margin) + view_margin + height + arg2;
LOG.E ("----33--after calculation---", "left=" +left+ ", right=" + Right + ", top=" +top ");
if (Left > right) {
left = width + view_margin + arg1;
row++;
top = row * (height + view_margin) + view_margin + height + arg2;
}
LOG.E ("----4444-----", "left=" + (Left-width) + ", top=" + (Top-height) + ", right=" + Left + ", bottom=" +top ";
Child.layout (Left-width-arg1, Top-height, LEFT-ARG1, top);
}
}
private int measurewidth (int pwidthmeasurespec) {
int result = 0;
int widthmode = Measurespec.getmode (PWIDTHMEASURESPEC);//Get mode
int widthsize = measurespec.getsize (PWIDTHMEASURESPEC);//Get size
Switch (widthmode) {
/**
* Mode A total of three cases, the value is measurespec.unspecified, measurespec.exactly,
* Measurespec.at_most.
*
*
* measurespec.exactly is the exact size,
* When we specify the Layout_width or Layout_height of a control as a specific value, such as Andorid
*: Layout_width= "50dip", or fill_parent Yes, is the size of the control has been determined, is the exact size.
*
*
* Measurespec.at_most is the largest size,
* When the layout_width or layout_height of a control is specified as Wrap_content
*, the size of the control generally varies with the control's subspace or content, so that the control size does not exceed the maximum size allowed by the parent control
* 。 Therefore, mode at this point is at_most,size gives the maximum size allowed by the parent control.
*
*
* Measurespec.unspecified is not a specified size, this situation is not much, is generally the parent control is Adapterview,
* The mode passed through the measure method.
*/
Case Measurespec.at_most:
Case measurespec.exactly:
result = Widthsize;
Break
}
return result;
}
private int measureheight (int pheightmeasurespec) {
int result = 0;
int heightmode = Measurespec.getmode (Pheightmeasurespec);
int heightsize = measurespec.getsize (Pheightmeasurespec);
Switch (heightmode) {
Case Measurespec.at_most:
Case measurespec.exactly:
result = Heightsize;
Break
}
return result;
}
}
2. The following methods are used in the XML file:
<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Vertical"
android:paddingleft= "20DP"
android:paddingright= "20DP" >
<com.snippet.worklearn.flowviewgroup
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:visibility= "Visible"
android:background= "#f0f" >
<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:background= "@drawable/aaa"
Android:gravity= "Center_vertical"
android:paddingleft= "20DP"
android:paddingright= "10DP"
android:text= "Two words"/>
<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:background= "@drawable/aaa"
android:gravity= "Center"
Android:text= "three characters"/>
<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:background= "@drawable/aaa"
android:gravity= "Center"
Android:text= "This is four words"/>
<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:background= "@drawable/aaa"
android:gravity= "Center"
android:text= "Five words exceed No"/>
<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:background= "@drawable/aaa"
android:gravity= "Center"
android:text= "Meche 345678"/>
<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:background= "@drawable/aaa"
android:gravity= "Center"
android:text= "One"/>
<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:background= "@drawable/aaa"
android:gravity= "Center"
Android:text= "Six words should be newline"/>
<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:background= "@drawable/aaa"
android:gravity= "Center"
Android:text= "Seven words must have changed"/>
<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:background= "@drawable/aaa"
android:gravity= "Center"
Android:text= "What's the result"/>
<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:background= "@drawable/aaa"
android:gravity= "Center"
android:text= "One"/>
<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:background= "@drawable/aaa"
android:gravity= "Center"
android:text= "hehe"/>
</com.snippet.worklearn.MyViewGroup2>
</LinearLayout>
3. Operation:
Android Streaming layout