Android屏幕适配的哪些事
为什么要屏幕适配?

device_framentation.png
统计
碎片化
基础概念
屏幕尺寸
屏幕尺寸是指屏幕对角线的长度,单位是英寸,1 inch=2.54 cm
屏幕分辨率
手机在横向和纵向上的像素点数总和,单位是像素(pixel),1px = 1像素点,举个栗子,1080x1920,即宽度方向上有1080个像素点,在高度方向上有1920个像素点。
屏幕像素密度
每英寸像素点个数,单位是dpi,dots per inch。为简便起见,Android 将所有屏幕密度分组为六种通用密度: 低、中、高、超高、超超高和超超超高。

dpi_example.png
屏幕密度无关像素dp(dip)
Density Independent Pixels,即密度无关像素。

density-test-bad.png
使用dp在低、中、高屏幕密度下的效果

density-test-good.png
独立比例像素sp
Scale Independent Pixels, 即sp或sip。
Android开发时用此单位设置文字大小,可根据字体大小首选项进行缩放,推荐使用12sp、14sp、18sp、22sp作为字体设置的大小,不推荐使用奇数和小数,容易造成精度的丢失问题,小于12sp的字体会太小导致用户看不清。
屏幕适配之图片适配

screens-densities.png
在设计图标时,对于5种主流的像素密度(mdpi,hdpi,xhdpi,xxhdpi和xxxdpi)应按照2:3:4:6:8的比例进行缩放。例如一个启动图片ic_launcher.png,它在各个像素密度文件夹下大小为:
Android SDK加载图片流程
那么应该提供哪种分辨率规格呢?
原则上越高越好,同时结合当前主流分辨率屏幕
自动拉伸图片

ninepatch_raw.png

ninepatch_examples.png
屏幕适配之布局适配
布局参数
使用wrap_content, match_parent, layout_weight。
weight的使用

weight_examples.png
宽度 = 原来宽度 权重比值 * 剩余宽度
使用相对布局,禁用绝对布局。
限定符
尺寸限定符

dimen_example1.png

dimen_example2.png
即使使用dp,依然不能解决屏幕分辨率的适配问题,我们可以针对不同的屏幕创建不同的dimen值。

newsreader_land.png

newsreader_port.png
当NewsReader在横屏时是双面板,左侧是HeadLinesFragment, 右侧是ArticleFragment, 点击新闻标题, 切换ArticleFragment的内容。当NewsReader在竖屏时是单面板,只有个HeadLinesFragment, 点击新闻标题,跳转到ArticleActivity去显示新闻内容。所以,要实现这样的横竖屏适配,只是通过布局是完成不了的,不同业务逻辑的处理,还需要写代码来完成,这就是我们的自适应用户界面。
使用布局别名
View articleView = findViewById(R.id.article);mIsDualPane = articleView != null && articleView.getVisibility() == View.VISIBLE;//如果能够找到ArticleFragment则是双面板单双面板的不同业务逻辑
public void onHeadlineSelected(int index) { mArtIndex = index; if (mIsDualPane) { // display it on the article fragment mArticleFragment.displayArticle(mCurrentCat.getArticle(index)); } else { // use separate activity Intent i = new Intent(this, ArticleActivity.class); i.putExtra("catIndex", mCatIndex); i.putExtra("artIndex", index); startActivity(i); }}
device_framentation.png
统计
碎片化
-
品牌机型碎片化
-
屏幕尺寸碎片化
-
操作系统碎片化
基础概念
屏幕尺寸
屏幕尺寸是指屏幕对角线的长度,单位是英寸,1 inch=2.54 cm
屏幕分辨率
手机在横向和纵向上的像素点数总和,单位是像素(pixel),1px = 1像素点,举个栗子,1080x1920,即宽度方向上有1080个像素点,在高度方向上有1920个像素点。
屏幕像素密度
每英寸像素点个数,单位是dpi,dots per inch。为简便起见,Android 将所有屏幕密度分组为六种通用密度: 低、中、高、超高、超超高和超超超高。
-
ldpi(低)~120dpi
-
mdpi(中)~160dpi
-
hdpi(高)~240dpi
-
xhdpi(超高)~320dpi
-
xxhdpi(超超高)~480dpi
-
xxxhdpi(超超超高)~640dpi
dpi_example.png
屏幕密度无关像素dp(dip)
Density Independent Pixels,即密度无关像素。
-
160dpi, 1dp = 1px
-
240dpi, 1dp = 1.5px
-
320dpi, 1dp = 2px
-
480dpi, 1dp = 3px
-
640dpi, 1dp = 4px
density-test-bad.png
使用dp在低、中、高屏幕密度下的效果
density-test-good.png
独立比例像素sp
Scale Independent Pixels, 即sp或sip。
Android开发时用此单位设置文字大小,可根据字体大小首选项进行缩放,推荐使用12sp、14sp、18sp、22sp作为字体设置的大小,不推荐使用奇数和小数,容易造成精度的丢失问题,小于12sp的字体会太小导致用户看不清。
屏幕适配之图片适配
screens-densities.png
在设计图标时,对于5种主流的像素密度(mdpi,hdpi,xhdpi,xxhdpi和xxxdpi)应按照2:3:4:6:8的比例进行缩放。例如一个启动图片ic_launcher.png,它在各个像素密度文件夹下大小为:
-
ldpi(低)
-
mdpi(中)48*48
-
hdpi(高)72*72
-
xhdpi(超高)96*96
-
xxhdpi(超超高)144*144
-
xxxhdpi(超超超高)192*192
-
每套分辨率出一套图,为美工或者设计增加了许多工作量
-
对Android工程文件的apk包变的很大
Android SDK加载图片流程
-
Android SDK会根据屏幕密度自动选择对应的资源文件进行渲染加载,比如说,SDK检测到你手机的分辨率是xhdpi,会优先到xhdpi文件夹下找对应的图片资源;
-
如果xhdpi文件夹下没有图片资源,那么就会去分辨率高的文件夹下查找,比如xxhdpi,直到找到同名图片资源,将它按比例缩小成xhpi图片;
-
如果往上查找图片还是没有找到,那么就会往低分辨率的文件夹查找,比如hdpi,直到找到同名图片资源,将它按比例放大成xhpi图片。
那么应该提供哪种分辨率规格呢?
原则上越高越好,同时结合当前主流分辨率屏幕
自动拉伸图片
ninepatch_raw.png
ninepatch_examples.png
屏幕适配之布局适配
布局参数
使用wrap_content, match_parent, layout_weight。
weight的使用
weight_examples.png
-
当layout_width为0dp,layout_weight分别是1和2
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="weight = 1"/> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:text="weight = 2"/> </LinearLayout>
-
当layout_width为match_parent,layout_weight分别为1和2
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="weight = 1"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="2" android:text="weight = 2"/> </LinearLayout>
宽度 = 原来宽度 权重比值 * 剩余宽度
-
当layout_width为0dp,layout_weight分别是1和2
第一个按钮:宽度 = 0 1/3 * 屏宽 = 1/3屏宽
第二个按钮:宽度 = 0 2/3 * 屏宽 = 2/3屏宽
-
当layout_width为match_parent, layout_weight分别是1和2
第一个按钮:宽度 = 屏宽 1/3 (屏宽 - 2 屏宽) = 2/3屏宽
第二个按钮:宽度 = 屏宽 2/3 (屏宽 - 2 屏宽) = 1/3屏宽
使用相对布局,禁用绝对布局。
限定符
尺寸限定符
-
在手机较小的屏幕上,加载layout文件夹布局
-
在平板电脑和电视的屏幕(>7英寸)上, 加载layout-large文件夹的布局
-
Android3.2版本之前
-
在手机较小的屏幕上,加载layout文件夹布局
-
标准7英寸平板(其最小宽度为 600 dp),加载layout-sw600dp文件夹的布局
-
在Android3.2版本及之后版本
-
适配手机的单面板(默认)布局:res/layout/activity_main.xml
-
适配尺寸>7寸平板的双面板布局(Android 3.2前):res/layout-large/activity_main.xml
-
适配尺寸>7寸平板的双面板布局(Android 3.2后):res/layout-sw600dp/activity_main.xml
-
适配手机的单面板(默认)布局:res/layout/activity_main.xml
-
适配尺寸>7寸平板的双面板布局:res/layout/activity_twopanes.xml
-
res/values/layout.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <item name="main" type="layout">@layout/activity_main</item> </resources>
-
res/values-large/layout.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <item name="main" type="layout">@layout/activity_twopanes</item> </resources>
-
res/values-sw600dp/layout.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <item name="main" type="layout">@layout/activity_twopanes</item> </resources>
-
setContentView(R.layout.main);
-
res/layout-land
-
res/layout-port
-
res/layout-sw600dp-land
-
res/layout-sw600dp-port
-
Nexus 4 (4.7英寸 768x1280:xhdpi)
dimen_example1.png
-
Nexus S (4英寸 480x800:hdpi)
dimen_example2.png
即使使用dp,依然不能解决屏幕分辨率的适配问题,我们可以针对不同的屏幕创建不同的dimen值。
-
res/values/dimens.xml
<resources> <dimen name="button_length_1">180dp</dimen> <dimen name="button_length_2">160dp</dimen> </resources>
-
res/values-480x800/dimens.xml
<resources> <dimen name="button_length_1">113dp</dimen> <dimen name="button_length_2">100dp</dimen> </resources>
-
官方文档
-
Github Sample
<?xml version="1.0" encoding="utf-8"?> <android.support.percent.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:text="30%" app:layout_widthPercent="30%"/> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:text="20%" app:layout_widthPercent="20%"/> </android.support.percent.PercentRelativeLayout>
newsreader_land.png
newsreader_port.png
当NewsReader在横屏时是双面板,左侧是HeadLinesFragment, 右侧是ArticleFragment, 点击新闻标题, 切换ArticleFragment的内容。当NewsReader在竖屏时是单面板,只有个HeadLinesFragment, 点击新闻标题,跳转到ArticleActivity去显示新闻内容。所以,要实现这样的横竖屏适配,只是通过布局是完成不了的,不同业务逻辑的处理,还需要写代码来完成,这就是我们的自适应用户界面。
使用布局别名
-
res/values/layouts.xml
<resources> <item name="main_layout" type="layout">@layout/onepane_with_bar</item> <bool name="has_two_panes">false</bool> </resources>
-
res/values-sw600dp-land/layouts.xml
<resources> <item name="main_layout" type="layout">@layout/twopanes</item> <bool name="has_two_panes">true</bool> </resources>
-
res/values-sw600dp-port/layouts.xml
<resources> <item name="main_layout" type="layout">@layout/onepane</item> <bool name="has_two_panes">false</bool> </resources>
View articleView = findViewById(R.id.article);mIsDualPane = articleView != null && articleView.getVisibility() == View.VISIBLE;//如果能够找到ArticleFragment则是双面板单双面板的不同业务逻辑
public void onHeadlineSelected(int index) { mArtIndex = index; if (mIsDualPane) { // display it on the article fragment mArticleFragment.displayArticle(mCurrentCat.getArticle(index)); } else { // use separate activity Intent i = new Intent(this, ArticleActivity.class); i.putExtra("catIndex", mCatIndex); i.putExtra("artIndex", index); startActivity(i); }}