(一)对哪些设备适配?
主流设备:
现在市面上占比最多的六种分辨率:480*800、320*480、480*854、540*960、720*1280、1080*1920。在日常适配中只要做好对这几个设备的适配,就能很好的适配其他机型
另外也需注意对安卓平板、安卓电视等大尺寸、超大尺寸设备的适配。
(二)适配的方案:
(1)要确保布局的灵活性并适应各种尺寸的屏幕,应使用 “wrap_content” 、“match_parent”和权重控制某些视图组件的宽度和高度
(2)使用相对布局,不要使用绝对布局。
我们大部分时候使用的都是线性布局、相对布局和帧布局,绝对布局由于适配性极差,所以极少使用。
(3)多套layout适配(只需要在res目录下创建不同的layout文件夹。)
(4)使用限定符进行适配操作
(5)图片处理方面:
1.使用.9图
2.logo(参考android目录中的小机器人所的分辨率)
logo需要36*36、48*48、72*72、96*96、144*144、192*192px,图片使用正方形形状,在某些机型上面,会自动显示为圆角正方形;
(6)尺寸使用dp ,字体大小使用sp
综合了一下目前主要的android适配方案主要有以下几种:
1.创建不同的资源文件
创建不同的资源文件 values-1920x1080,values-1280x720等等
2.百分比布局支持库
使用谷歌正式提供的百分比布局支持库(percent-support-lib),它提供两种布局
PercentFrameLayout,PercentRelativeLayout
支持的属性有:
layout_widthPercent
layout_heightPercent
layout_marginPercent
layout_marginLeftPercent
layout_marginTopPercent
layout_marginRightPercent
layout_marginBottomPercent
layout_marginStartPercent
layout_marginEndPercent
例子:PercentRelativeLayout
<?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="match_parent"
android:clickable="true">
<TextView
android:id="@+id/row_one_item_one"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_alignParentTop="true"
android:background="#7700ff00"
android:text="w:70%,h:20%"
android:gravity="center"
app:layout_heightPercent="20%"
app:layout_widthPercent="70%"/>
<TextView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_below="@id/row_two_item_one"
android:background="#770000ff"
android:gravity="center"
android:text="width:100%,height:10%"
app:layout_heightPercent="10%"
app:layout_widthPercent="100%"/>
</android.support.percent.PercentRelativeLayout>
但是百分比库还是存在一些问题:
当使用图片时,无法设置宽高的比例
比如我们的图片宽高是200*100的,我们在使用过程中我们设置宽高为20%、10%,这样会造成图片的比例失调。为什么呢?因为20%参考的是屏幕的宽度,而10%参考的是屏幕的高度。
2.很难使用百分比定义一个正方形的控件
比如,我现在界面的右下角有一个FloatingActionButton,我希望其宽度和高度都为屏幕宽度的10%,很难做到。
3.一个控件的margin四个方向值一致
有些时候,我设置margin,我希望四边的边距一致的,但是如果目前设置5%,会造成,上下为高度的5%,左右边距为宽度的5%。
综合上述这些问题,可以发现目前的percent-support-lib并不能完全满足我们的需求,
3.Android AutoLayout适配方式
一款第三方的支持库
AutoLayout所用单位px:这里的px并非是Google不建议使用的px,在内部会进行转化处理。
支持的属性:
layout_width
layout_height
layout_margin(left,top,right,bottom)
pading(left,top,right,bottom)
textSize
maxWidth, minWidth, maxHeight, minHeight
目前还在完善支持的布局和view。
详细的配置和使用详见https://github.com/hongyangAndroid/AndroidAutoLayout
综上分析
从程序的稳定适用性个人觉得还是通过创建不同的资源文件来适配比较稳妥。
---以上个人根据网络资源总结整理,经验不足,欢迎多多指教---