1、为什么要适配
Android 的 系统碎片化、机型以及屏幕尺寸碎片化、屏幕分辨率碎片化严重,我们急需一种更优适配方式能够让界面视图在每个设备上能够正常显示。
2、适配相关知识概念
物理像素点(px) : 代表的是手机的物理像素点
密度无关像素(dp): 密度无关像素等于160dpi 屏幕上的一个物理像素,这是系统的基线密度
屏幕密度(dpi) : 屏幕每英寸上的像素点,Android 将所有的屏幕密度分组为6中通用密度:低(ldpi), 中(mdpi), 高(hdpi), 超高(xhdpi),xxhdpi 和 xxxhdpi.
获取设备物理像素大小1280px X 800px
adb shell wm size
获取设备DPI=213(这个比较奇怪命令叫density)
adb shell wm density
Android屏幕适配-基础篇
Android屏幕适配必备知识
3、最小宽度限定符
计算最小宽度
smallestWidth = min(H,W)/(DPI/160)
按标题2中的举例的像素(1280px X 800px)和DPI(213)举例(这是我真是开发中遇到的平板Android设备)。
-
min(H,W):最小宽度不区分方向以宽高最小的作为最小宽度。
-
设计图按1280 X 800单位可以理解为你自己的单位 比如:dpp
-
例如:1280dp(px这里直接认为1dp = 1px) X 800dp(px)的真实smallestWidth = 800/(213/160) = 600dp
-
生成文件夹: values-sw600dp
-
生成比例为: 600dp/设计图最小宽度 1dpp = 600dp/800dpp = 0.75dp
-
所以1dpp就换算为0.75dp
<dimen name="dpp1">0.7500dp</dimen>
...
<dimen name="dpp10">7.5000dp</dimen>
使用ScreenMatch生成不同宽度的文件夹
############################################################################
# Start with '#' is annotate. #
# In front of '=' is key, cannot be modified. #
# More information to visit: #
# http://blog.csdn.net/fesdgasdgasdg/article/details/52325590 #
# http://download.csdn.net/detail/fesdgasdgasdg/9913744 #
# https://github.com/mengzhinan/PhoneScreenMatch #
############################################################################
#
# 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 !!!!!!!!! _^_ *_*
base_dp=800
# Also need to match the phone screen of [match_dp].
# If you have another dp values.
# System default values is 384,392,400,410,411,480,533,592,600,640,662,720,768,800,811,820,960,961,1024,1280,1365
match_dp=600,1080
# If you not wanna to match dp values above. Write some above values here, append value with "," .
# For example: 811,961,1365
ignore_dp=384,392,400,410,411,432,480,533,592,640,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
# System default values is .gradle, gradle, .idea, build, .git
ignore_module_name=
# Use which module under the values/dimen.xml file to do the base file,
# and generated dimen.xml file store in this module?
# Default value is 'app'.
match_module=app
# Don't show select dialog again when use this plugin.
# System screen match will use the last selected module name or default module name.
# You can give value true or false. Default value is false.
not_show_dialog=false
# Do you want to generate the default example dimens.xml file?
# In path of .../projectName/screenMatch_example_dimens.xml, It does not affect your project code.
# You can give value true or false. Default value is false.
not_create_default_dimens=false
# Does the font scale the same size as the DP? May not be accuracy.
# You can give value true or false. Default value is true. Also need scaled.
is_match_font_sp=false
# Do you want to create values-wXXXdp folder or values-swXXXdp folder ?
# I suggest you create values-swXXXdp folder,
# because I had a problem when I was working on the horizontal screen adapter.
# values-swXXXdp folder can solve my problem.
# If you want create values-swXXXdp folder, set "create_values_sw_folder=true",
# otherwise set "create_values_sw_folder=true".
# Default values is true.
create_values_sw_folder=true
其他适配方式
- dp直接适配
- 宽高限定符
- 修改density(今日头条方案)