Android screen adaptation rules and android screen adaptation
To adapt to different screens, the first thing we need is to be able to distinguish different screens. android provides several different dimensions for limitation (qualifier, some translated as qualified words)
Some common dimensions are shown, but not all. All dimensions can be found in the official list.
In the figure, the priority is reduced from left to right according to the direction indicated by the arrow (priority is used for searching and matching resources), and when the res sub-directory is named with a qualified word, it must also be named according to this priority, for example, a res-sw600dp-port is correct and a compilation error occurs for the res-port-sw600dp. Some dimensions can have many different specific parameters, such as smallestWidth. Some dimensions can only have fixed options, such as language and port/land on the screen.
The following describes each dimension:
1,Language (zh-rCN)The two-letter language code defined in ISO 639-1
2,SmallestWidth shortest available width(Sw-<N> dp), which is added from android3.2, indicating the shortest available width of the device, regardless of the screen direction. However, this parameter involves some components on the screen. If there is a navigation bar on the screen, the minimum available width may not be the value of the shortest side of the screen, this parameter indicates the available UI intervals.
Adaptation process: when multiple directories use this qualifier, the matching principle is not greater than the maximum available width. That is, if the minimum side of the screen is 1536, the screen density is xhdpi (that is, 320 dpi), 1536/2 = 768, then select the maximum value less than or equal to 768 in all sw-<N> dp, that is, search for matching in the lower direction.
3,Available width (w-<N> dp)Similar to smallestWidth, but related to the screen direction, downward match
4,ScreenSize screen sizeThe Unit is inch-inch. It can be divided into small/normal/large/xlarge. The adaptation sequence is from high to low. If it is defined in a directory that exceeds the current size, crash. This dimension is not recommended since android3.2. We recommend using shortestWidth.
5,Screen directionThis is the simplest, vertical
6,Screen density(Dots per inch), ldpi/mdpi/hdpi/xhdpi/xxhdpi respectively correspond to screen density of 120/160/240/320/480/640 dpi, when placing resources, the resource size should also be proportional. (Determine the number of pixel represented by 1dp ).
Adaptation process: first, find the directories with the current screen density matching. If not, continue to look up the layer (tend to zoom down the large image, rather than zoom in the small image). If the upper layer does not, search for the lower layer, if no directory exists, the default directory is used. After a specific directory is located, the default directory is scaled based on the ratio of the located directory to the current screen density.
7,Resolution (800*480)But it is still usable. The priority is between nav and platform, and the adaptation sequence is from high to low.
8,Platform version: See Api Levels. The adaptation sequence also ranges from high to low.
Android resource matching process:
Best practice:
1. provide different layout and dimen for different screen sizes and directions
2. provide different drawable for different screen density dpi
3. provide default resources to prevent crash
4. When modifying a value in a directory, check whether the value is configured on the upper layer of the directory. If yes, you can directly modify the value. If no value exists, copy the value of this layer to the upper layer, and then modify the value of this layer.
Android official:
Https://developer.android.com/guide/topics/resources/providing-resources.html
Https://developer.android.com/guide/practices/screens_support.html