屏幕适配全攻略

一、概念

1.屏幕尺寸:指屏幕的对角线的长度,单位英寸,1英寸=2.54厘米。

2.屏幕分辨率:指在横纵向上的像素点数。单位px,1px = 1像素点,一般以纵向像素*横向像素,如1920*1080。

3.屏幕像素密度:指每英寸上的像素点数,单位dpi,与屏幕尺寸和屏幕分辨率有关。

4.px:构成图像的最小单位。

5.dp,dip:密度无关像素,以160dp为基准,1dip = 1px。

6.sp:可以根据文字大小首选项进行放缩,建议选择大于等于12sp,不要用奇数和小数,避免精度丢失。

屏幕密度            图标尺寸                    像素密度范围

mdpi                  48x48px                    120dp~160dp

hdpi                   72x72px                    160dp~240dp

xhdpi                 96x96px                    240dp~320dp

xxhdpi               144x144px                 320dp~480dp

xxxhdpi             192x192px                 480dp~640dp

给大家推荐一个网址,Android Design中文版:http://adchs.github.io/style/

二、屏幕适配

1.支持各种屏幕尺寸

a.使用wrap_content、match_parent、weight

b.使用相对布局,禁用绝对布局(精确控制子控件之间的位置)

c.使用限定符

d.使用自动拉伸位图:.9图片拉伸

weight计算公式:原来宽度(布局设置的layout_width) + 剩余空间所占半分比的宽度

限定符:

尺寸限定符:large限定符  res/layout/main.xml普通手机屏幕    res/layout-large/main.xml平板屏幕 (Android 3.2之前使用)

最小宽度限定符:res/layout/main.xml普通手机屏幕    res/layout-sw600dp/main.xml平板屏幕 (Android 3.2之前,之后使用)

布局别名:为了把布局相同的部分抽取出来,单独进行管理,如下面左边的可以写成右边的布局

res/layout/main.xml                           单面板布局                        res/layout/main.xml                       单面板布局        

res/layout-large/main.xml                  多面板布局        ----》        res/layout/main_twopanes.xml      多面板布局

res/layout-sw600dp/main.xml            多面板布局

默认布局

res/values/layout.xml:

<resources>

    <item name="main"  type="layout">@layout/main</item>

</resources>

Android3.2之前的平板布局

res/values-large/layout.xml:

<resources>

    <item name="main"  type="layout">@layout/main_twopanes</item>

</resources>

Android3.2之后的平板布局

res/values-sw600dp/layout.xml:

<resources>

    <item name="main"  type="layout">@layout/main_twopanes</item>

</resources>

屏幕方向限定符

res/values-sw600dp-land/layouts.xml:横屏

<resources>

    <item name="main"  type="layout">@layout/main_twopanes</item>

</resources>

res/values-sw600dp-port/layouts.xml:竖屏

<resources>

    <item name="main"  type="layout">@layout/main_twopanes</item>

</resources>






猜你喜欢

转载自blog.csdn.net/b13646141775/article/details/79775263