[Sorting out several common dimensions in the]android

Source: Internet
Author: User

Get three code forms for screen width and height dimensions

On Android, I now know that there are three different code forms for getting the screen size

Method 1: The most common invocation method in activity

WindowManager WindowManager = Getwindowmanager ();

Display display = Windowmanager.getdefaultdisplay ();

int screenwidth = Display.getwidth ();

int screenheight = Display.getheight ();

Since the above-mentioned getwidth and getheight including Getorientation have become @deprecated after API level 13, the screen width is replaced with getsize, and there is a method 2

Point outsize = new Point ();

Display.getsize (outsize);

ScreenWidth = outsize.x;

ScreenHeight = Outsize.y;

Obviously to do API level discrimination feel uncomfortable, then there is a third general form of

Displaymetrics dm = new Displaymetrics ();

Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM);

ScreenWidth =dm.widthpixels;

ScreenHeight =dm.heightpixels;

In fact, the above three kinds of code, the call is WindowManager the default Display object inside the width of the high size, some places said that these three forms in the screen is not displayed when the actual value is not taken, will get all 0 of the situation, In the OnCreate function of activity, I find that it is possible to fetch a specific value, even if it is not in the activity, through application's OnCreate function, the following code:

((WindowManager) Getsystemservice (Context.window_service)). Getdefaultdisplay (). Getmetrics (DM);

Also can get the specific screen width high value.

Get the size of the status bar

Since Decorview is the topmost view in window, you can get to decorview from window and Decorview have a getwindowvisibledisplayframe method to get to the area displayed by the program. Includes the title bar, but does not include the status bar.

So there's the first form of code that calculates the height of the status bar.

Rect frame = new Rect ();

GetWindow (). Getdecorview (). Getwindowvisibledisplayframe (frame);

int statusbarheight = Frame.top;

The above code is sometimes obtained to the height of 0, the online version of the 4.0.3 after the return of 0, I did not test, but on my real machine actually got is 0, online out of a source program to get the height of the status bar code:

Statusbarheight = Getresources (). Getdimensionpixelsize (Com.android.internal.r.dimen.status_bar_height);

So how do you get it in the non-source? The online popular solution, is to the above need source code realization way through reflection to achieve the status bar height acquisition, the specific code is as follows:

Import Java.lang.reflect.Field;

Get Phone status bar height

public static int Getstatusbarheight (context context) {

Class<?> c = null;

Object obj = null;

Field field = NULL;

int x = 0, statusbarheight = 38;//Usually this value will be 38

try {

c = Class.forName ("Com.android.internal.r$dimen");

obj = C.newinstance ();

field = C.getfield ("Status_bar_height");

x = Integer.parseint (Field.get (obj). toString ());

Statusbarheight = Context.getresources (). getdimensionpixelsize (x);

} catch (Exception E1) {

E1.printstacktrace ();

}

return statusbarheight;

}

After testing that the function is hidden in the status bar when the value is the same as the status bar display value is the same, does not change, and in the screen test, also do not change, that is, the height of the status bar on the phone should be fixed.

The above method is a reference to the online to get the status bar height of the post came

Http://www.cnblogs.com/renkangke/archive/2013/05/27/3101375.html

Get title bar height

Above said to get the status bar, incidentally, how to get the title bar height of the problem, I now know that the method to get the height of the title bar is not directly used in the OnCreate function, must be called after the UI is displayed, specifically the layout after the call, the specific code is as follows:

int contenttop = GetWindow (). Findviewbyid (window.id_android_content). GetTop ();

Statusbarheight is the height of the status bar that is asked above

int titlebarheight = Contenttop–statusbarheight;

The principle of the above method is GetWindow (). Findviewbyid (Window.id_android_content) This method gets to the view that the program does not include the title bar section. Since it cannot be called directly in the OnCreate function, some calling methods are to start a thread in oncreate and then delay the call.

I do not know what method can be more reliable to get the title bar height, if there is any technology cattle people know also hope to inform, in addition this method and other controls in the Contentview, the size of the same, in the oncreate is not directly obtained, direct Access will be 0.

In addition, we use specific pixel values in the code, the specific pixel values are device-related, we generally do not use the specific pixel value directly in the code, but rather in the resource file to define the device-independent DP value, and then obtain a specific pixel value by a similar method
int padding = Getresources (). Getdimensionpixelsize (r.dimen.broadcast_select_padding);
Sometimes there is a specific numerical method to get the pixel value, such as the above r.dimen.broadcast_select_padding if it is 16DP, you can get the pixel value by the following method
Displaymetrics DM = new Displaymetrics ();
Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM);
int padding = dm.scaleddensity
But this method is not recommended because Android has too many devices, and on some phones this multiplication gets the value with Getresources (). The values obtained by getdimensionpixelsize are not equal, while the latter is certainly more accurate than the former.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.