Android gets the width and height of the mobile phone compatible with different Android devices

Source: Internet
Author: User

[Java] // The first method returns pixels.
Display display = getWindowManager (). getDefaultDisplay ();
Log. I ("MainActivity", "height:" + display. getHeight ());
Log. I ("MainActivity", "width:" + display. getWidth ());
// The second method returns pixels.
DisplayMetrics displayMetrics = getResources (). getDisplayMetrics ();
Log. I ("MainActivity", "height" + displayMetrics. heightPixels );
Log. I ("MainActivity", "width" + displayMetrics. widthPixels );

// The first method returns pixels.
Display display = getWindowManager (). getDefaultDisplay ();
Log. I ("MainActivity", "height:" + display. getHeight ());
Log. I ("MainActivity", "width:" + display. getWidth ());
// The second method returns pixels.
DisplayMetrics displayMetrics = getResources (). getDisplayMetrics ();
Log. I ("MainActivity", "height" + displayMetrics. heightPixels );
Log. I ("MainActivity", "width" + displayMetrics. widthPixels );


In fact, in general, you do not need to know the width or height of the mobile phone. if you know the height of the mobile phone, and then make various if else or swith judgments, this code is very bad. In fact, at the beginning of Android design, the resolution of different devices is different. How can this problem be solved? Apart from distinguishing image folders like drawable-hdpi and drawable-ldpi, you can also create values-hdpi and values-ldpi, or create a folder Based on the width and height of a specific device, for example, values-800x480 and values-960x540 in. Note: x is a lowercase letter x, not a multiplication number, and must be preceded by a large number.

 

 


The csdn image is added with a watermark...

 

Related Article

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.