文章目录

前言

对于Android不同分辨率的适配,网上内容一大堆,但还是觉得自己简单整理一下,加深一下印象。哈哈哈,纯流水账,为了更新博客,大佬的话可以不用看哈。

好记性不如烂笔头

正文

对于解决这个适配的内容,其实懂的话很简单的,但是对于不懂的确实是丈二的和尚摸不着头脑。下面我简单记录一下,当然也是参考AI的建议,然后自己总结的。

直入正题,

先打印一下设备信息

private void initTest() {
    DisplayMetrics metrics = getResources().getDisplayMetrics();
    Configuration config = getResources().getConfiguration();
    Log.d(TAG, "initTest 设备屏幕分辨率: " + metrics.widthPixels + "x" + metrics.heightPixels);
    Log.d(TAG, "initTest 设备屏幕密度: " + metrics.density);
    Log.d(TAG, "initTest 设备屏幕dpi: " + metrics.densityDpi);
    Log.d(TAG, "initTest 设备高度(dp): " + metrics.heightPixels);
    Log.d(TAG, "initTest 设备宽度(dp): " + metrics.widthPixels);
    Log.d(TAG, "initTest 设备最小宽度: " + config.smallestScreenWidthDp);
    Log.d(TAG, "initTest 当前设备支持布局配置: " + getResources().getConfiguration());
}

设备1

adb shell wm size显示的分辨率1920x1080

设备屏幕dpi为160

设备屏幕分辨率: 1920x984
设备屏幕密度: 1.0
设备屏幕dpi: 160
设备高度(dp): 984
设备宽度(dp): 1920
设备最小宽度: 1080
当前设备支持布局配置: {1.0 ?mcc?mnc [zh_CN] ldltr sw1080dp w1920dp h914dp 160dpi xlrg long land car finger -keyb/v/h -nav/h winConfig={ mBounds=Rect(0, 0 - 1920, 1080) mAppBounds=Rect(0, 0 - 1920, 984) mMaxBounds=Rect(0, 0 - 1920, 1080) mDisplayRotation=ROTATION_0 mWindowingMode=fullscreen mDisplayWindowingMode=fullscreen mActivityType=standard mAlwaysOnTop=undefined mRotation=ROTATION_0} s.2 fontWeightAdjustment=0}

设备屏幕dpi为320

设备屏幕分辨率: 1920x984
设备屏幕密度: 2.0
设备屏幕dpi: 320
设备高度(dp): 984
设备宽度(dp): 1920
设备最小宽度: 540
当前设备支持布局配置: {1.0 ?mcc?mnc [zh_CN] ldltr sw540dp w960dp h422dp 320dpi nrml long land car finger -keyb/v/h -nav/h winConfig={ mBounds=Rect(0, 0 - 1920, 1080) mAppBounds=Rect(0, 0 - 1920, 984) mMaxBounds=Rect(0, 0 - 1920, 1080) mDisplayRotation=ROTATION_0 mWindowingMode=fullscreen mDisplayWindowingMode=fullscreen mActivityType=standard mAlwaysOnTop=undefined mRotation=ROTATION_0} s.2 fontWeightAdjustment=0}

设备2

adb shell wm size显示的分辨率1920x720

设备屏幕分辨率: 1808x720
设备屏幕密度: 1.0
设备屏幕dpi: 160
设备高度(dp): 720
设备宽度(dp): 1808
设备最小宽度: 608
当前设备支持布局配置: {1.0 ?mcc?mnc [zh_CN] ldltr sw608dp w1808dp h622dp 160dpi lrg long land car finger -keyb/v/h -nav/h winConfig={ mBounds=Rect(0, 0 - 1920, 720) mAppBounds=Rect(112, 0 - 1920, 720) mMaxBounds=Rect(0, 0 - 1920, 720) mDisplayRotation=ROTATION_0 mWindowingMode=fullscreen mDisplayWindowingMode=fullscreen mActivityType=standard mAlwaysOnTop=undefined mRotation=ROTATION_0} s.2 fontWeightAdjustment=0}

分析

从上面可以看出,尽管设备高度宽度一样,但存在设备屏幕dpi不同,导致其使用的layout或values也不同。

细节懒得分析了,继续可以问AI,直接看怎么配置layout。

重点来了,直接看当前设备支持布局配置那行打印的。

以设备2为例

sw608dp w1808dp h622dp

就是表示适配从上面的最小宽,宽,高等限定参数中找相近的,如果没有就使用默认的。

如果要制定设备2显示的布局,就需要按照上面的参数配置一下。

小结

下面针对Layout的配置,具体如下

layout
layout-sw608dp
layout-sw1080dp

设备1中设备屏幕dpi为160显示layout-sw1080dp

设备1中设备屏幕dpi为320显示layout (没有指定的就找默认的)

设备2显示layout-sw608dp

感觉好复杂哈,尤其是做手机的适配~_~

参考文章

  1. 《[]dpi 、 dip 、分辨率、屏幕尺寸、px、density 关系以及换算

  2. 腾讯元宝AI》

相关文章

暂无评论

评论审核已启用。您的评论可能需要一段时间后才能被显示。

none
暂无评论...