First, about Layout_margin
Android time is not too short. To Layout_margin is not unfamiliar, but recently encountered a problem let me find that it is not enough to understand the depth of comprehensive. A large amount of network data says that Layout_margin refers to the distance of the view from the parent view. This statement is not rigorous enough. The right word is. The distance from the view's relative view is more accurate.
Under LinearLayout, you can feel the distance from the parent view.
But under the relativelayout is not. Suppose view A is already written on the right side of view B. The layout_marginleft of view A is the distance from view B, regardless of the parent view. Also, is the distance between two view centers or the distance between the two sides? Be able to understand this. Each view is a rectangular area, assuming that view a is already set on the right side of view B, then Layout_marginleft is the distance between view B's right and the left side of view a, not the distance between the two view centers!
For example, the following:
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvewfuemkxmji1nji3/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center "/>
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvewfuemkxmji1nji3/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center "/>
Ii. questions about your own definition of a composite control layout () is not valid
As you can see. I combine a imageview with a textview as a self-defined control. Then put four of these own defined controls in a relativelayout at the bottom. Both sides of the view, "message" and "settings" can be set by the relativelayout left and right padding to control the position. But the second view and the third view need to be adjusted.
The bottom of this relativelayout code such as the following:
<?xml version= "1.0" encoding= "Utf-8"? ><org.yanzi.ui.bottomcontrolpanel xmlns:android= "/http Schemas.android.com/apk/res/android "android:layout_width=" match_parent "android:layout_height=" 60DP "Android:lay Out_alignparentbottom= "true" android:gravity= "center_vertical" android:paddingleft= "20DP" android:paddingright= "2 0DP "> <org.yanzi.ui.imagetext android:id=" @+id/btn_message "android:layout_width=" Wrap_content " android:layout_height= "Wrap_content" android:layout_alignparentleft= "true"/> <org.yanzi.ui.imagetext Android:id= "@+id/btn_contacts" android:layout_width= "wrap_content" android:layout_height= "Wrap_conte NT "android:layout_torightof=" @id/btn_message "/> <org.yanzi.ui.imagetext android:id=" @+id/btn_news "Android:layout_width=" wrap_content "android:layout_height=" Wrap_content "android:layout_torightof=" @id/btn_contacts "/> <org.yanzi.ui.Imagetext android:id= "@+id/btn_setting" android:layout_width= "wrap_content" android:layout_height= "WR Ap_content "android:layout_alignparentright=" true "/></org.yanzi.ui.bottomcontrolpanel>
Here I set the second view on the right side of the first view. The third view is on the right side of the second one. This bottomcontrolpanel.java is also defined by itself and inherits from Relativelayout. To allow four view equidistant distributions, instinctively think of rewriting Bottomcontrolpanel.java's onlayout () function. Note here that the second view "contact" is wider than the other 3 view widths.
@Overrideprotected void OnLayout (Boolean changed, int left, int top, int. right, int bottom) {//TODO auto-generated method Stubsuper.onlayout (changed, left, top, right, bottom), Layoutitems (left, top, right, bottom),//for (int i = 0; i< N; i++ ) {//imagetext v = viewlist.get (i);//getchildat (i);//v.setvisibility (view.visible);//int vtop = Top;//int VLeft = Paddingleft + I * (blankv + vieww)//int Vbottom = Bottom;//int vright = vleft + vieww;//(v). Layout (Vleft, Vtop, Vright, V Bottom);//LOG.I ("Yanzi", "vtop =" + Vtop + "Vleft =" + Vleft + "Vbottom =" + Vbottom + "Vright =" + vright);//}}/** The leftmost and rightmost view is controlled by the padding of the parent layout. The location of the 2nd, 3 view will be set again * @param left * @param top * @param right * @param bottom */private void layoutitems (int left, int t OP, int right, int bottom) {int n = getchildcount (); if (n = = 0) {return;} int paddingleft = Getpaddingleft (); int paddingright = Getpaddingright (); LOG.I ("Yanzi", "paddingleft =" + paddingleft + "paddingright =" + paddingright); int width = right-Left;int height = bottom-top; LOG.I ("Yanzi", "width =" + width + "height =" + height); int allviewwidth = 0;for (int i = 0; i< N; i++) {View v = getchildat (i); LOG.I ("Yanguoqi", "v.getwidth () =" + V.getwidth ()); Allviewwidth + = V.getwidth ();} int blankwidth = (width-allviewwidth-paddingleft-paddingright)/(n-1); LOG.I ("Yanzi", "blankv =" + blankwidth); Layoutparams params1 = (layoutparams) viewlist.get (1). Getlayoutparams ();p arams1.leftmargin = Blankwidth;viewlist.get (1). Setlayoutparams (PARAMS1); Layoutparams params2 = (layoutparams) viewlist.get (2). Getlayoutparams ();p arams2.leftmargin = Blankwidth;viewlist.get (2). Setlayoutparams (PARAMS2);}
At first I was using this paragraph of the onlayout () function to control:
//for (int i = 0; i< N; i++) {
//Imagetext v = viewlist.get (i); Getchildat (i);
//V.setvisibility (view.visible);
//int vtop = top;
//int vleft = paddingleft + I * (blankv + vieww);
//int vbottom = bottom;
//int vright = Vleft + vieww;
//(v). Layout (Vleft, Vtop, Vright, Vbottom);
//LOG.I ("Yanzi", "vtop =" + Vtop + "Vleft =" + Vleft + "Vbottom =" + Vbottom + "Vright =" + vright);
//}
Obviously, a view's drawing process is divided into: onmeasure to measure the size, onlayout the position of the calculation, OnDraw to draw. But here, layout () This function does not work.
I used to use layout () is good, attention to use layout () when the above code super.onlayout (changed, left, top, right, bottom); It was the default process that ran with super (). View can also be drawn out.
Get rid of this super. Replace it with my own computed coordinate layout (). The result is that a view is not drawn. Yes, it is. For a single layout, such as replacing the imagetext here with ImageView, it would be able to place it normally, and for that I rewrote the Imagetext onlayout () function. There is still nothing to draw out.
Print discovery via OnLayout (). Two times in the display process. For the first time, the width and height of the view are 0. It should be not shown yet, and the second time it will be width and height. (This descriptive narrative is inaccurate, and it needs to be studied carefully)
Finally, in order to solve this problem, simply let it super.onlayout (), under him add their own written layoutitems () function. The position is determined by calculating the leftmargin. Then there is the first question on the precise understanding of margin.
Finally, the problem is solved perfectly, the horizontal screen can also be placed normally:
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvewfuemkxmji1nji3/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center "/>
Finally, add one point. A view hypothesis sets the padding, so the length of View.getwidth () is included in the padding.
The process of Layoutitems () is to obtain the width of the entire parent view, which includes the leftmost and rightmost two padding. Then calculate the Allviewwidth, which is the total width of the four view. Subtract the two by 3 to get the distance between the 3 slots, and then set the parameters down to OK. Until I comb the source code and then upload.
Android:layout_margin the real meaning and the definition of the composite Control layout () run invalid problem resolution