Android development, many people will meet the full line to automatically change to the next line of the interface requirements, and Android self-linearlayout layout can be horizontal or vertical row, not enough to display on add ScrollView, mixed platoon is not. Here we share a linearlayout that can be used for automatic line wrapping.
Import java.util.Hashtable;
Import Android.content.Context;
Import Android.util.AttributeSet;
Import Android.util.Log;
Import Android.view.View;
Import Android.widget.LinearLayout;
/**
* LinearLayout of Automatic line wrapping
* @author Idengpan
*
*/
@SuppressWarnings ({"Unchecked", "Rawtypes"})
public class Autonextlinelinearlayout extends LinearLayout {
int Mleft, Mright, Mtop, Mbottom;
Hashtable map = new Hashtable ();
Public Autonextlinelinearlayout (Context context) {
Super (context);
}
Public Autonextlinelinearlayout (context context, int horizontalspacing, int verticalspacing) {
Super (context);
}
Public Autonextlinelinearlayout (context context, AttributeSet Attrs) {
Super (context, attrs);
}
@Override
protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {
int mwidth = measurespec.getsize (Widthmeasurespec);
int mCount = Getchildcount ();
int MX = 0;
int MY = 0;
Mleft = 0;
mright = 0;
Mtop = 5;
Mbottom = 0;
int j = 0;
View Lastview = null;
for (int i = 0; i < MCount; i++) {
Final View child = Getchildat (i);
Child.measure (measurespec.unspecified, measurespec.unspecified);
This increases the line-break judgment in OnLayout, which is used to calculate the desired height
int CHILDW = Child.getmeasuredwidth ();
int CHILDH = Child.getmeasuredheight ();
MX + = CHILDW; Adds a statistical overlay to the width of each child control and requires a newline if it is greater than the set height, and the height or top coordinate must be reset
position Position = new Position ();
Mleft = getPosition (i–j, i);
Mright = Mleft + child.getmeasuredwidth ();
if (MX >= mwidth) {
mx = CHILDW;
MY + = CHILDH;
J = i;
Mleft = 0;
Mright = Mleft + child.getmeasuredwidth ();
Mtop = MY + 5;
//PS: If you find the height or have a problem, you have to fine tune the
}
Mbottom = Mtop + child.getmeasuredheight ();
MY = Mtop; Each height must be recorded otherwise the control will be superimposed together
Position.left = Mleft;
Position.top = Mtop + 3;
Position.right = mright;
Position.bottom = Mbottom;
Map.put (child, position);
}
Setmeasureddimension (Mwidth, Mbottom);
}
@Override
Protected Layoutparams Generatedefaultlayoutparams () {
return new Layoutparams (0, 0); Default of 1px spacing
}
@Override
protected void OnLayout (Boolean changed, int l, int t, int r, int b) {
int count = Getchildcount ();
for (int i = 0; i < count; i++) {
View child = Getchildat (i);
Position pos = (Position) map.get (child);
if (pos! = null) {
Child.layout (Pos.left, Pos.top, Pos.right, Pos.bottom);
} else {
LOG.I ("Mylayout", "error");
}
}
}
Private class Position {
int left, top, right, bottom;
}
public int getPosition (int indexinrow, int childindex) {
if (Indexinrow > 0) {
Return GetPosition (indexinrow–1, childindex–1) + Getchildat (childindex–1). Getmeasuredwidth () + 8;
}
return Getpaddingleft ();
}
}
The usage is similar to normal linearlayout, where the full path to the custom class is written in the XML layout file, and the Android:orientation property is specified as horizontal (default).
Android source Sharing-line wrap LinearLayout