This article describes the common methods of obtaining the width and height of View in Android development. Share it for your reference, as follows:
1. Obtained according to WindowManager Manager
1) These two methods are still in the 0 state when the screen is not displayed, that is, they must be insetContentViewIt will only be valid after the call.
2) Activity must be set in this way to obtain the width and height of the view
//Set to UntitledrequestWindowFeature(Window.FEATURE_NO_TITLE); //Set to full screen mode getWindow().setFlags(.FLAG_FULLSCREEN,.FLAG_FULLSCREEN
1) Old version
WindowManager windowManager = getWindowManager(); Display display = (); screenWidth = (); screenHeight = ();
2) New version
DisplayMetrics dm = new DisplayMetrics(); ().getDefaultDisplay().getMetrics(dm);//this refers to the current activityscreenWidth =; screenHeight =;
2. Obtained by canvas in onDraw
@Override protected void onDraw(Canvas canvas) { (canvas); screenWidth =(); screenHeight =(); }
3. Rewrite the onSizeChanged of the view. This method will be called after onCreate and before onDraw.
protected void onSizeChanged(int w, int h, int oldw, int oldh) { viewHeight=h; viewWidth=w; (w, h, oldw, oldh); }
For more information about Android related content, please check out the topic of this site:Android View View Tips Summary》、《Summary of Android graphics and image processing skills》、《Android development introduction and advanced tutorial》、《Android debugging skills and solutions to common problems》、《Android multimedia operation skills summary (audio, video, recording, etc.)》、《Summary of the usage of basic Android components》、《Android layout layout tips summary"and"Android control usage summary》
I hope this article will be helpful to everyone's Android programming design.