Android adaptive screen orientation, size, and resolution supplement

Source: Internet
Author: User

Screen Size
1. Different Layout

The screen sizes of Android mobile phones vary by 480X320,640x360,800 X. How can an app automatically adapt to different screens? In fact, it is very simple, just need to create different layout folder under the res directory, such as: layout-640x360, layout-800x480, all the layout files will be written to R after compilation. java, and the system selects the appropriate layout based on the screen size.

2. hdpi, mdpi, and ldpi

In earlier versions, there was only one drawable, while in version 2.1, there were three types: drawable-mdpi, drawable-ldpi, and drawable-hdpi, which were mainly used to support multi-resolution.

Differences between drawable-hdpi, drawable-mdpi, and drawable-ldpi:

1. The drawable-hdpi contains high-resolution images, such as WVGA (480x800) and fwvga (480X854)
2. drawable-mdpi stores medium-resolution images, such as hvga (320x480)
3. drawable-ldpi stores low-resolution images, such as qvga (240x320)
The system will find the corresponding images in these folders according to the machine resolution. To be compatible with different screens on different platforms, we recommend that you store images of different versions in different folders as needed.

Screen direction
1. Automatic horizontal and vertical screen Switching

You can create two layout-port and layout-land directories under the res directory, which are placed in two layout files: vertical screen and horizontal screen, in this way, the system automatically calls the corresponding layout file when the screen direction of the mobile phone changes, to avoid the problem that one layout file cannot meet the two screen display conditions.

2. Disable Automatic Switch

You only need to add the Android: screenorientation attribute restriction to the androidmanifest. xml file.

• Android: screenorientation = "Landscape" // restrict the horizontal display of this page
• Android: screenorientation = "portrait" // restrict the number of portrait screens on this page
Adaptive font size
Method 1:

First, different font sizes are obtained based on different resolutions.
Create in res
In values-480x320/strings. XML, set <dimen name = "text_size"> 30px </dimen>
And
In values-800x400/strings. XML, set <dimen name = "text_size"> 30px </dimen>

The font size of 480x320 and X is 30px and 40px respectively;

This is called in a Java file.

Int sizeoftext = (INT) This. getresources (). getdimension (R. dimen. text_size );

Method 2:

Obtain the view width in onsizechanged of the view. Generally, the default width is 320. Therefore, calculate a zoom rate = (float) W/320 W to indicate the actual width.
And then paint when setting the font size. settextsize (INT) (8 * rate); 8 is the actual font size to be set when the resolution width is 320 = default font size x rate

---------------------------------------- The above is transferred, and the following is my own pipeline --------------------------------------------------------------------------------------------

First, create an XML file for the corresponding screen, such as 480X320,800x400

The following is the directory structure:

 

Second, you must add the following code in androidmanifest. xml.

Set the following points to OK...

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.