Android Adaptive screen orientation, size and resolution, and font settings

Source: Internet
Author: User

Screen size

1. Different layout

Android phone screen size is different, there are 480x320,640x360,800x480. How can I get the app to adapt to various screens automatically? In fact, it is very simple to create a different layout folder in the Res directory, such as: layout-640x360,layout-800x480, all layout files will be compiled after compilation Write to the R.java, and the system will choose the appropriate layout to use according to the size of the screen.

2, hdpi, MDPI, ldpi

In the previous version, there was only one drawable, while the 2.1 version had drawable-mdpi, drawable-ldpi, drawable-hdpi Three, and the three were mostly to support multi-resolution.

The difference between drawable-hdpi, drawable-mdpi and drawable-ldpi:

    1. DRAWABLE-HDPI contains high-resolution images, such as WVGA (480x800), FWVGA (480x854)
    2. DRAWABLE-MDPI contains medium-resolution images such as HVGA (320x480)
    3. DRAWABLE-LDPI contains low-resolution images, such as QVGA (240x320)

The system will be based on the resolution of the machine to each of these folders to find the corresponding picture. In developing the program in order to be compatible with different screens on different platforms, it is recommended that the respective folders store different versions of the images according to requirements.

Screen orientation

1, Horizontal screen vertical screen automatically switch

The Res directory can be set up in the Layout-port and Layout-land two directories, which are placed in vertical screen and horizontal screen two layout files, so that the mobile phone screen direction changes when the system will automatically call the corresponding layout files, to avoid a layout file can not meet the two screen display problems.

2. Disable automatic switching

You only need to include the Android:screenorientation attribute limit in the Androidmanifest.xml file.

    • android:screenorientation= "Landscape"//is to limit the horizontal screen display of this page
    • android:screenorientation= "Portrait"//is to limit the number of pages displayed on the vertical screen
Font self-adapting size

Method 1:

First, you get different font sizes based on different resolutions.
Create in Res
Values-480x320/strings.xml inside set <dimen name= "Text_size" >30px</dimen>
And
Values-800x400/strings.xml inside set <dimen name= "Text_size" >30px</dimen>

For 480x320 and 800x400 resolution respectively, the font size is 30px and 40px;

This is called in the Java file

int sizeoftext = (int) this.getresources (). Getdimension (R.dimen.text_size);

Method 2:

Get the view width in the onsizechanged of the view, normally the default width is 320, so calculate a zoom rate = (float) w/320 W is the actual width
Then, when setting the font size paint.settextsize ((int) (8*rate)); 8 is the font size to be set at a resolution width of 320 the actual font size = default font size x rate

Android Adaptive screen orientation, size and resolution, and font settings

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.