Android屏幕适配(二)多分辨率布局适配策略

来源:互联网 发布:淘宝天堂伞假货 编辑:程序博客网 时间:2024/05/01 21:16

Android布局的适配方法有很多种,不过每一种方法的思路无非就是让不同分辨率的手机去读各自的适配文件,主要总结下面两种方式:
1、每个分辨率下面一套layout,比如layout-480x800,layout-1280x720,1080x1080等。
这种方法的缺点就是产品需要改点需求的时候,每一个layout文件都需要去修改,增加了适配的工作量。
2、每个分辨率下面一套values文件夹,里面是不同的dimens.xml,定义不同的尺寸去适配。
如果是按分辨率去适配,往往我们需要新建的dimens.xml比较多。

在给手机屏幕适配的时候有下面几点需要注意的地方:
1、使用dpi 屏幕密度去适配。
2、公共的部分大小和字体的大小都应该统一去定义,比如标题栏、文章内容字体大小等等。
3、非主流的分辨率、厂商改变过density的手机,我们都可能要做单独的处理。

density和分辨率的关系表
屏幕级别
屏幕密度
比率(相对)
物理大小(英寸)
像素大小
通常的分辨率
ldpi
120
3
0.75
1
120

mdpi
160
4
1
1
160
320*480
hdpi
240
6
1.5
1
240
480*800
xhdpi
320
8
2
1
320
720*1280
xxhdpi
480
12
3
1
480
1080*1800
xxxhdpi
640
16
4
1
648
3840×2160
hdpi 比率是1,其他的按照这个比率可以计算出demins的值,当屏幕密度是160,1dp=1px,
这下我们可以建自己的demins.xml来适配



valuse-ldpi,valuse-mdpi,valuse-hdpi,valuse-xhdpi,valuse-xxhdpi,valuse-xxxhdpi

每一个都按照上面表中的对照在dimens.xml定义尺寸,比如 mpdi <dimen name="title">10sp</dimen>,那么hdpi就应该是<dimen name="title">15sp</dimen>
dimens我们可以预先把所有的尺寸都定义上


这样基本上能适配市场上大部分的主流手机,那么遇到一些非主流的情况,比如上面讲的1280*720,inchit=6.0,density却是1.5,
如果你只是做了上面的dpi适配的话,这款手机的布局适配不是xhdpi,而是hdpi,也就是走了480*800分辨率的dpi。
那么该怎么去解决这个问题呢?
dpi+分辨率,新建values-hdpi-1280x720 ,这样系统就会去找到这个适配文件去适配。

valuse-xxxhdpi   4.3以后出现的4K屏幕的适配
values-sw720dp-lan width=720dp的平板适配
values-nodpi-1280×720  所有密度这个分辨率的适配


官方适配的几张图片,便于理解
http://developer.android.com/guide/practices/screens_support.html
Screen characteristicQualifierDescriptionSizesmallResources for small size screens.normalResources for normal size screens. (This is the baseline size.)largeResources for large size screens.xlargeResources for extra-large size screens.DensityldpiResources for low-density (ldpi) screens (~120dpi).mdpiResources for medium-density (mdpi) screens (~160dpi). (This is the baseline density.)hdpiResources for high-density (hdpi) screens (~240dpi).xhdpiResources for extra-high-density (xhdpi) screens (~320dpi).xxhdpiResources for extra-extra-high-density (xxhdpi) screens (~480dpi).xxxhdpiResources for extra-extra-extra-high-density (xxxhdpi) uses (~640dpi). Use this for the launcher icon only, see note above.nodpiResources for all densities. These are density-independent resources. The system does not scale resources tagged with this qualifier, regardless of the current screen's density.tvdpiResources for screens somewhere between mdpi and hdpi; approximately 213dpi. This is not considered a "primary" density group. It is mostly intended for televisions and most apps shouldn't need it—providing mdpi and hdpi resources is sufficient for most apps and the system will scale them as appropriate. If you find it necessary to provide tvdpi resources, you should size them at a factor of 1.33*mdpi. For example, a 100px x 100px image for mdpi screens should be 133px x 133px for tvdpi.OrientationlandResources for screens in the landscape orientation (wide aspect ratio).portResources for screens in the portrait orientation (tall aspect ratio).Aspect ratiolongResources for screens that have a significantly taller or wider aspect ratio (when in portrait or landscape orientation, respectively) than the baseline screen configuration.notlongResources for use screens that have an aspect ratio that is similar to the baseline screen configuration.
























0 0
原创粉丝点击