In OnCreate () using View.getwidth () or view.getheiht () to get the width and height of the view, it seems that there is no problem, actually they go to the value is 0, not the result you want? What is this for? When the OnCreate () method is called, the interface is not visible and the memory loading component has not been drawn, and you are unable to get his size. How can you get the size of the component before you draw it? Here are three methods, validated: Method one://measurement method int width =view.measurespec.makemeasurespec (0,view.measurespec.unspecified); int height = View.MeasureSpec.makeMeasureSpec (0,view.measurespec.unspecified); View.measure (width,height); int height= View.getmeasuredheight (); int Width=view.getmeasuredwidth (); Method two//increase the component before drawing the listener Viewtreeobserver vto =view.getviewtreeobserver (); Vto.addonpredrawlistener (New Viewtreeobserver.onpredrawlistener () {@Override public booleanonpredraw () {int Height =view.getmeasuredheight (); int width =view.getmeasuredwidth (); }}); Method Three://increase overall layout monitoring viewtreeobserver vto = View.getviewtreeobserver (); Vto.addongloballayoutlistener (New Ongloballayoutlistener () {@Override public voidongloballayout () {View.get Viewtreeobserver (). Removeglobalonlayoutlistener (this); int height =VIEW.GEtmeasuredheight (); int width =view.getmeasuredwidth (); } }); Then, when the activity enters the runtime, the component's size acquisition method is simple, directly getwidth () and GetHeight
Android gets view width and height