In a custom ListView, you need to set the padding of the drop-down refresh to be hidden when the view is initialized, and then get the height of the layout view to load at initialization time.
Private View Headview;headview = inflater.inflate (R.layout.header, NULL);
If you next call:
Headview.getheight (); Headview.getmeasuredheight ();
We know will return 0, because getmeasuredheight to measure after the value, getheight to the layout after the value. To get the width of the loaded layout here, the programmer calls measure to measure the width of the layout. Before writing down the ListView when there is such a piece of code, has not quite understood, today studied a bit:
private void Measureview (View child) {viewgroup.layoutparams LP = Child.getlayoutparams (); if (LP = = NULL) {LP = new Viewgrou P.layoutparams (ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);} Headerview width information int childmeasurewidth = viewgroup.getchildmeasurespec (0, 0, lp.width); int childmeasureheight;if ( Lp.height > 0) {childmeasureheight = Measurespec.makemeasurespec (Lp.height, measurespec.exactly);//The last parameter means: fit, match } else {childmeasureheight = Measurespec.makemeasurespec (0, measurespec.unspecified);//Unspecified}//system.out.println (" Childviewwidth "+childmeasurewidth");//system.out.println ("Childviewheight" +childmeasureheight);// Set the width and height to childchild.measure (childmeasurewidth, childmeasureheight);}
After inflate the layout, call Measureview (Headview) and then call the Getmeasureheight function to get the actual height.
The next step is to analyze the code in principle:
by inflater.inflate (R.layout.header, NULL), when the XML is converted to view, the Layoutparams without setting the view will go to LP = = The null condition in the new one layoutparams. The next set of view Childmeasurewidth and Childmeasureheight is confusing.
Childmeasurewidth:
The first argument passed by the Getchildmeasurespec of the calling ViewGroup (the spec of the parent class) is 0, Passing in 0 indicates that the spec for the parent class is an unspecified measurespec.unspecified pattern, and the size is 0. Then the value returned by the function is a value of unspecified type size 0.
Childmeasureheight:
Similar to the above, if Lp.height > 0 is set to an exact value, next encapsulates a value of exactly and Lp.height to Childmeasureheight, and here lp.height is wrap_content, He is a negative (-2), so it encapsulates a value that does not specify unspecified and size 0 to Childmeasureheight.
That is, the Childmeasurewidth and childmeasureheight two parameters are 0.
The Widthmeasurespec and Heightmeasurespec passed to the child can be seen by the printed results.
The two values are then passed as arguments to the measure function of the child view.
The last time I saw this in Zhang Hongyang's blog, I called the Mpopupwindow.getcontentview () in the swipe delete code to get Popupwindow's width. Measure (0, 0); Get his wide height.
View view = Minflater.inflate (r.layout.delete_btn, null); MDELBTN = (Button) View.findviewbyid (R.ID.ID_ITEM_BTN); Mpopupwindow = new Popupwindow (view, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_ CONTENT); /** * First call the next measure, otherwise you will not get the width and height */ Mpopupwindow.getcontentview (). Measure (0, 0); Mpopupwindowheight = Mpopupwindow.getcontentview (). Getmeasuredheight (); Mpopupwindowwidth = Mpopupwindow.getcontentview (). Getmeasuredwidth ();
In this case, it is actually the same code that was given at the beginning. Since the analysis of the starting code, we found that the last execution of Child.measure (Childmeasurewidth, Childmeasureheight), the time two parameters is 0.
I initially had difficulty understanding the parent layout passed to the child layout with a value of unspecified and 0. Because, by default, it is set directly for the View :
Setmeasureddimension (Getdefaultsize (Getsuggestedminimumwidth (), Widthmeasurespec), getdefaultsize ( Getsuggestedminimumheight (), heightmeasurespec));
The functions in front of the function have been studied, it is obvious that the passing of the 0 set to the view. But after calling this code, the size of the real thing is not 0.
with the question, I will measure the layout just to write a view, named View_layout.xml:
<view xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "50DP" Android : layout_height= "50DP" android:background= "#ffff0000" />
There is also a view wrapped by LinearLayout, Viewgroup_layout.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 " ><view android:layout_width= "50DP" android:layout_height= "50DP" /></ Linearlayout>
There is also a te Xtview_layout.xml:
<textview xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "50DP " android:layout_height= "50DP" android:text= "Test" />
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:orientation= "vertical" android:id= "@+id/mainlayout" ></LinearLayout>
Then execute the following code when Oncerate:
Inflater = Layoutinflater.from (this); Mainlayout = (linearlayout) Findviewbyid (r.id.mainlayout); View = Inflater.inflate (r.layout.view_layout, null); TextView = inflater.inflate (r.layout.textview_layout, null); ViewGroup = inflater.inflate (r.layout.viewgroup_layout, null); Measureview (view); Measureview (TextView); Measureview (viewgroup); LOG.I (TAG, "view width and height respectively:" + view.getmeasuredwidth () + " " + view.getmeasuredheight ()); LOG.I (TAG, "textview width and height respectively:" + textview.getmeasuredwidth () + " " + textview.getmeasuredheight ()); LOG.I (TAG, "viewgroup width and height respectively:" + viewgroup.getmeasuredwidth () + " " + viewgroup.getmeasuredheight ());
The resulting results are:
It was exactly the same as the original explanation. For view, it is set directly by default.
The measured values are obtained for TextView and viewgroup that inherit from view.
Where is the reason? Because TextView and ViewGroup both rewrite the onmeasure. At_most and unspecified are treated the same way in ViewGroup and TextView.
It has been overlooked that unspecified is seldom used, so it is not possible to find problems. It is not inertial to think that onmeasure inside the operation is called Setmeasureddimension to save the value of a wide height. Before this, if it is viewgroup will also be specific according to the situation of the inside sub-view processing.
Now that we've talked about the dimensions of the view, we'll add them to the parent layout and get the following measurement widths, and the width and height measurements will be in onwindowfocuschanged.
Mainlayout.addview (view); Mainlayout.addview (TextView); Mainlayout.addview (ViewGroup) ; @Override public void Onwindowfocuschanged (Boolean hasfocus) { log.i (tag, "after gaining focus");// LOG.I (Tag, " The width and height of the view are: "+ view.getmeasuredwidth () +" "+ view.getheight ()); LOG.I (TAG, "textview width and height respectively:" + textview.getmeasuredwidth () + " " + textview.getmeasuredheight ()); LOG.I (TAG, "viewgroup width and height respectively:" + viewgroup.getmeasuredwidth () + " " + viewgroup.getmeasuredheight ()); Super.onwindowfocuschanged (Hasfocus);}
The printed log is as follows:
The width of the view is worth it! What is the reason? When calling AddView, it will try to redraw! As for the width and height why is this, that is because the call Generatedefaultlayoutparams returns the width of match_parent high for wrap_content. The final measurement of the view is that the onmeasure parameters are At_most and screensize. Because it is a view, call Setmeasureddimension settings directly.
The reason behind two appears 0 is that view fills the screen. If you do not call Mainlayout.addview (view), you can see:
After changing to "horizontal", print the following information:
As for height, the TextView is 15, because it is the outermost layer and ultimately the package content. The final width of the viewgroup is 50DP, because my cell phone pixel density is 0.75, so the conversion to 38 pixels, the result is consistent.
This tells me that I call onmeasure only temporarily get the view of the wide-height measurement, this value with the incoming parent layout of Widthmeasurespec, Heightmeasurespec. Actually the actual width added to the layout may not be the same as this value.
A similar approach is found in the ListView, which is basically the same as the starting method.
private void MeasureItem (View child) { Viewgroup.layoutparams p = child.getlayoutparams (); if (p = = null) { p = new Viewgroup.layoutparams ( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); } int childwidthspec = Viewgroup.getchildmeasurespec (Mwidthmeasurespec, mlistpadding.left + mListPadding.right, P.width); int lpheight = p.height; int childheightspec; if (Lpheight > 0) { Childheightspec = Measurespec.makemeasurespec (Lpheight, measurespec.exactly); } else { childheightspec = Measurespec.makemeasurespec (0, measurespec.unspecified); } Child.measure (Childwidthspec, Childheightspec); }
How Android Gets the width of the loaded layout when initializing