Android adapts to the heart and virtues summarized by itself & adapts to mobile phones and tablets at the same time & adapts to horizontal and vertical screen switching at the same time

Foreword : Many units have been designed on this issue, such as px, pt, dp, sp, cell phone size, ppi, etc., I will only introduce the ones that I have solved. If you have a better screen adaptation solution, please leave a link in the comment area. thank you very much

1. Minimum width qualifier (recommended)

What is the minimum width qualifier, first look at my screenshot:

That's right, the folder ending with values-sw360dp is at the same level as the values ​​file in your project.

I simply use my words to understand: the unit recommended by Google is dp (just like the unit recommended by Apple is pt), which means screen density. Just hear the density. Although the resolutions of the major android sizes are many, they are very few when measured by density. In simple terms: the pixels are the same in a 1-inch screen

1.1. Thanks to the Great God plugin ScreenMatch

Detailed link, thanks .

When you can use the ScreenMatch plug-in, let me talk a little about the screenMatch.properties file, which is automatically generated by the plug-in:

Click to open is like this

############################################################################
#
# You need to refresh or reopen the project every time you modify the configuration,
# or you can't get the latest configuration parameters.
#
#############################################################################
#
# Base dp value for screen match. Cut the screen into [base_dp] parts.
# Data type is double. System default value is 360.
# I advise you not to modify the value, be careful !!!!!!!!! _^_  *_*
# 设计图上最小的宽度的基准值。意思就是满屏时是360dp
base_dp=360
# Also need to match the phone screen of [match_dp].
# If you have another dp values.
# System default values is 320,360,384,392.7272,400,410,411.4285,432,480,533,592,600,640,662,720,768,800,811,820,960,961,1024,1280,1365
# 要适配的最小宽度有哪些宽度
match_dp=320,360,384,392.7272,400,410,411.4285,432,480,533,592,600,640
# If you not wanna to match dp values above. Write some above values here, append value with "," .
# For example: 811,961,1365
# 需要忽略适配的宽度有哪些
ignore_dp=662,720,768,800,811,820,960,961,1024,1280,1365
# They're not android module name. If has more,split with , Symbol.
# If you set, it will not show in SelectDialog.
# If you have, write here and append value with "," .
# For example: testLibrary,commonModule
  • base_dp=360 Here is the dp value of the full screen on your design. As long as it is set and adapted on the phone, the size of the control can follow the design drawing. Super convenient
  • match_dp=320,360,384,392.7272,400,410,411.4285,432,480,533,592,600,640 What are the minimum widths to be adapted
  • ignore_dp=662,720,768,800,811,820,960,961,1024,1280,1365 Which widths need to be ignored

1.2, compatible with mobile phones & tablets at the same time

First of all, we need to know one thing, and to adapt to mobile phones and tablets, there should be two sets of layouts. Make a set of mobile phones and a set of tablets. It's easy to know this. We all know that the size of a mobile phone screen is generally between 3 inches and 5 inches, and the size of a tablet computer is between 7 inches and 10 inches. So the phone will directly run the layout in the layout. We define another layout-sw600dp folder, which is the same layer as layout. The layout inside is the tablet layout. Of course you have to be more refined and can negotiate different size folders. As follows

: If it runs on a mobile phone, it will use layout. Running on the tablet will go layout-sw600dp. You can put 2 sets of different layouts to try. But the xml name of your layout should be consistent.

I first run it on the phone and then on the tablet. The same project, see the effect


1.3, adapt to both horizontal and vertical screens

When you use the screenMatch plug-in, are there many folders with minimum width qualifier swXXdp generated? I also mentioned the minimum width qualifier before, which does not distinguish between horizontal and vertical screens, and the minimum side is the minimum width. Then it will be confusing when switching between horizontal and vertical screens. This means that you set a control when the screen is in portrait orientation, which occupies half of the screen width, but when you switch to the landscape screen, it does not occupy half. How to do it? ?

Remove the s of the swXXdp in front of the folder generated by the plugin and turn it into wXXdp. , The general meaning, the current direction is the minimum width direction, here is a distinction between horizontal and vertical screens. Look at the following effect (the configuration is done here, I did nothing, just rotated the phone to see the adaptation effect):

I'll mention it here, so I won't go into details. If you want to adapt a single set for horizontal or vertical screens, you can use horizontal and vertical qualifiers

  • values-sw400dp-land (minimum width 400 dp horizontal)
  • values-sw400dp-port (minimum width 400 dp vertical)

About the above demo link

2. Screen resolution qualifier (use with care)

What is the screen resolution qualifier?

First of all thanks to Zhang Hongyang for the automatic generation tool

But after my detailed test, I found that the problem is big. First of all, this is the disadvantage of the screen resolution qualifier. Let’s look at my test first, the real test machine I use,

Obviously my phone has a resolution of 1080 x 1920. Here we can analyze it from lay_x.xml. First of all, I used the width 375 in the design drawing to generate these folders.
Then I put two controls in the Activity, one set to @dimen/x375, the other set to @dimen/x187. But the result is not, one full screen, one half screen. After that I was very strange so I printed the current length of @dimen/x375 and the current width of the screen in Activity. The result is this:

Found that the screen width is indeed 1080. But the value of @dimen/x375 is indeed 800. Then I searched in the values ​​folder, and found that the values-1280x800 folder is 800px. But I am a little strange. Now that there is a values-1920x1080 folder. Why not go? ? ! ! I angrily deleted the values-1280x800 folder later. The result is values-1280x768. I was shocked, shocked. I was wondering whether some of my previous projects were cheating. ! ! Continue to delete the values-1280x768 folder later. As a result, I deleted it all the time, and the system kept looking up. Just don't find 1920x1080. There is no way to continue to find information about this mechanism. Link directly

Misused screen resolution qualifier
Android screen adaptation, a more complete solution

Personally suggest that the screen resolution qualifier should be used carefully. Unlike screenMathch that can withstand the test. If you have a better screen adaptation method, please leave a link in the comment section. Let's make progress together. 3q

3. Today's headline screen adaptation scheme

Today’s Toutiao Screen Adaptation Program Ultimate Edition, a very low-cost Android screen adaptation program ByteDance

official article
Summary: After actual testing and a large number of virtual machine tests, Toutiao uses 360dp as an example and 160dpi as an example. Try it without changing the code. After setting 360dp, if the mobile phone is 160dpi, it is normal, but the dpi is not 160, but the screen is not full. And if you get the dpi dynamically: appDisplayMetrics.densityDpi. But found that it still can not achieve the effect as stated in the official website article. Did I overlook something? And all it platform is based on official website articles as examples, 360 and 160. Have you really tested it with multiple models? I hope someone who understands better can solve my doubts! ! !

Guess you like

Origin blog.csdn.net/leol_2/article/details/100928303