Why screen adaptation? Fragmentation
In order to ensure that users get a consistent user experience and that a certain element has the same display effect on Android phones of different sizes and resolutions, we need to perform screen adaptation. Basic Concepts Screen size Screen size refers to the length of the screen diagonal, measured in inches, 1 inch = 2.54 cm Screen resolution The total number of pixels in the horizontal and vertical directions of the mobile phone. The unit is pixel. 1px = 1 pixel. For example, 1080x1920 means there are 1080 pixels in the width direction and 1920 pixels in the height direction. Screen pixel density The number of pixels per inch is measured in dpi, dots per inch. For simplicity, Android groups all screen densities into six general densities: low, medium, high, extra high, extra extra high, and extra extra extra high.
dpi_example Screen density independent pixel dp (dip) Density Independent Pixels, that is, density-independent pixels.
density-test-bad
dimen_example2 Independent scale pixel sp Scale Independent Pixels, also known as sp or sip. This unit is used to set the text size during Android development. It can be scaled according to the font size option. It is recommended to use 12sp, 14sp, 18sp, and 22sp as the font size. It is not recommended to use odd numbers and decimals, which can easily cause loss of precision. Fonts smaller than 12sp will be too small for users to see clearly. Screen adaptation and picture adaptation When designing icons, the five mainstream pixel densities (mdpi, hdpi, xhdpi, xxhdpi and xxxdpi) should be scaled in the ratio of 2:3:4:6:8. For example, a launcher image ic_launcher.png has the following sizes in each pixel density folder:
Problems
Workaround Android SDK loading picture process
According to the process of loading pictures, it can be concluded that theoretically it is enough to provide a set of pictures. So which resolution specification should be provided? In principle, the higher the better, combined with the current mainstream resolution screen Automatically stretch images ninepatch_raw ninepatch_examples Screen adaptation layout adaptation Layout parameters Use wrap_content, match_parent, layout_weight. Use of weight weight_examples
When layout_width is match_parent, layout_weight is 1 and 2 respectively
Calculation of weight Width = original width + weight ratio * remaining width
First button: width = 0 + 1/3 * screen width = 1/3 screen width Second button: width = 0 + 2/3 * screen width = 2/3 screen width
First button: Width = screen width + 1/3 (screen width - 2 screen widths) = 2/3 screen width Second button: Width = screen width + 2/3 (screen width - 2 screen width) = 1/3 screen width Layout Usage Use relative layout and disable absolute layout. Qualifiers Size Qualifiers
Minimum Width Qualifier
Layout Aliases
The XML contents of the two files are exactly the same, which will lead to duplication of file names and a series of maintenance problems. When modifying one file, you may forget to modify the other. So in order to solve this duplication problem, we introduced layout aliases.
Screen orientation qualifiers
Screen adaptation: dimen adaptation
Nexus S (4 inches 480x800: hdpi) dimen_example2 Even if we use dp, it still cannot solve the problem of screen resolution adaptation. We can create different dimen values for different screens.
Screen adaptation percentage layout
Screen Adaptation - Adaptive User Interface newsreader_land newsreader_port When NewsReader is in landscape mode, it has two panels, with HeadLinesFragment on the left and ArticleFragment on the right. Clicking on a news title switches the content of ArticleFragment. When NewsReader is in portrait mode, it has a single panel, with only HeadLinesFragment. Clicking on a news title switches to ArticleActivity to display the news content. Therefore, to achieve such landscape and portrait adaptation, it is not possible to achieve it only through layout. Different business logic processing also requires writing code to complete it. This is our adaptive user interface. Using layout aliases
Determine whether it is a single-sided or double-sided panel
Different business logic of single and double panels
|
<<: How to use ADB, the Android development debugging tool
>>: Android dynamic loading: DL framework
If I don't take a nap, I'm afraid I'l...
Training course content: "365 Days of Childr...
Q: How to rename an unnamed WeChat applet? A: How...
This article was reviewed by Tao Ning, PhD, Assoc...
As a gathering place for 2D people, Bilibili has ...
This article focuses on the disassembly of activi...
Recently, a high-definition spy photo of LeTV'...
The main factors affecting the price of mini prog...
Introduction: As the saying goes, "One mothe...
Can you do good Internet marketing at 0 cost? cer...
Recently, Nature published a paper on "assem...
On January 9, 2007, Apple CEO Steve Jobs unveiled...
This article will explain the significance of Xia...
Spring Blossoms Itchy nose? Cool facts about poll...
Core Tips 1. International crude oil market In Ap...