Because of the linear layout of line wrapping used in the previous time project, it was intended to use the table layout to add ImageView in Java code, but the view control added was indeterminate, because the data returned by the server In this way, manual Java code to draw the layout of this kind of premature, because in the cousin layout I can not determine how many imageview the number of rows, so can not be dynamically added, and finally self-can see the kind of linear layout can be changed, the linear layout is not good is not automatic line-wrapping, That is, when the Orentation setting LinearLayout is set to vertical in the vertical direction and only one column, each row can only display one view or the subclass of the view. When the orentitation of the LinearLayout is set to Horizontal,linearlayout, it can only be displayed as a single line, horizontally, and when the screen is full, the view control does not wrap itself. So all we have to do is wrap the linearlayout when it's full.
What you need to know is how to draw the width and height of the parent control based on the length and width of the child controls, so you need to pass in the height of the parameter control, and the view is divided into two types, one for the view type, the Textview,button,edittext for the control, and so on. There is also a container control that is loaded with views that inherit from ViewGroup controls, such as Linearlayout,relativelayout,tabhost, and so on, that require a linear layout of line wrapping, depending on the height and width of the child controls, To dynamically load the height and width of the parent control, you need to pass in the constructor a fixed height for each child control, or set the height and width of the child controls dynamically.
The custom LinearLayout also inherits from the ViewGroup and overrides several methods of the abstract class Viewgrouop: Onmeasure (), OnLayout (), Dispathdraw () The meanings of the three methods are: The first onmeasure () is used to calculate the area occupied by the control and the child controls, and the second onlayout () is a wrapping of the control child control, and the third one is writable, which is used primarily to draw the border of the control.
The code for the custom LinearLayout is as follows:
Package com.huanglong.mylinearlayout; import Android.content.context;import Android.graphics.canvas;import Android.graphics.color;import Android.graphics.paint;import Android.graphics.rect;import Android.util.attributeset;import Android.view.view;import Android.view.ViewGroup; /** * @author huanglong 2013-5-28 custom Wrap LinearLayout*/ Public classFixgridlayout extends ViewGroup {Private intMcellwidth; Private intMcellheight; PublicFixgridlayout (Context context) {super (context); } PublicFixgridlayout (Context context, AttributeSet Attrs) {Super (context, attrs); } PublicFixgridlayout (context context, AttributeSet attrs,intDefstyle) {Super (context, attrs, Defstyle); } Public voidSetmcellwidth (intW) {mcellwidth=W; Requestlayout (); } Public voidSetmcellheight (inth) {Mcellheight=h; Requestlayout (); } /** * Control line breaks for child controls*/@Overrideprotected voidOnLayout (Boolean changed,intLintTintRintb) {intCellwidth =Mcellwidth; intCellheight =Mcellheight; intcolumns = (r-l)/Cellwidth; if(Columns <0) {Columns=1; } intx =0; inty =0; inti =0; intCount =Getchildcount (); for(intj =0; J < Count; J + +) {Final View Childview=Getchildat (j); //gets the child control's width height intW =childview.getmeasuredwidth (); inth =childview.getmeasuredheight (); //to calculate the vertex coordinates of a child control intleft = x + ((cellwidth-w)/2); inttop = y + ((cellheight-h)/2); //int left = x; //int top = y; //Layout child controlsChildview.layout (left, top, left + W, top +h); if(I >= (Columns-1) ) {i=0; X=0; Y+=Cellheight; } Else{i++; X+=Cellwidth; } } } /** * Calculate the control and the area occupied by the child controls*/@Overrideprotected voidOnmeasure (intWidthmeasurespec,intHeightmeasurespec) { //Creating measurement Parameters intCellwidthspec =Measurespec.makemeasurespec (Mcellwidth, measurespec.at_most); intCellheightspec =Measurespec.makemeasurespec (Mcellheight, measurespec.at_most); //record the total number of child in ViewGroup intCount =Getchildcount (); //set subspace child's width height for(inti =0; I < count; i++) {View Childview=Getchildat (i); /** 090 This was called to find out how big a view should be. 091 the * Parent Supplies Constrai NT information in the width and height * parameters. 092 The actual mesurement work of a view are performed * in onmeasure (int, int.), 093 called by this method. 094 Therefore, * only onmeasure (int, int) can and must is overriden by subclasses. * 095*/childview.measure (Cellwidthspec, Cellheightspec); } //To set the area size of a container control//note the use of setmeasureddimension and ResolvesizeSetmeasureddimension (Resolvesize (Mcellwidth *count, Widthmeasurespec), Resolvesize (Mcellheight*count, Heightmeasurespec)); //setmeasureddimension (Widthmeasurespec, Heightmeasurespec); //methods that do not need to call the parent class//super.onmeasure (Widthmeasurespec, heightmeasurespec); } /** * Add a border to a control*/@Overrideprotected voidDispatchdraw (canvas canvas) {//Get layout control width height intwidth =getwidth (); intHeight =getheight (); //Create a brushPaint Mpaint =NewPaint (); //set individual properties of a brushMpaint.setcolor (Color.Blue); Mpaint.setstyle (Paint.Style.STROKE); Mpaint.setstrokewidth (Ten); Mpaint.setantialias (true); //Create a rectangle boxRect Mrect =NewRect (0,0, width, height); //Draw BorderCanvas.drawrect (Mrect, mpaint); //Finally, the method of the parent class must be calledSuper.dispatchdraw (canvas); } }
Then reference your own defined control in the XML file, called in the Java code:
Package com.huanglong.mylinearlayout; import Android.os.bundle;import Android.app.activity;import Android.view.menu;import Android.view.menuitem;import Android.widget.checkbox;import Android.widget.SimpleAdapter ; Import Android.support.v4.app.NavUtils; Public classMainactivity extends Activity {PrivateSimpleadapter Adapter; @Override Public voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Fixgridlayout Fixgridlayout=(fixgridlayout) Findviewbyid (R.ID.LL); Fixgridlayout.setmcellheight ( -); Fixgridlayout.setmcellwidth ( -); for(inti =0; I <7; i++) {CheckBox box=NewCheckBox (mainactivity. This); Box.settext ("Section"+i+"a"); Fixgridlayout.addview (box); }} @Override Publicboolean Oncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.activity_main, menu); return true; } }
Effect:
Android LinearLayout Automatic Wrapping