JessYanCoding / AndroidAutoSize

🔥 A low-cost Android screen adaptation solution (今日头条屏幕适配方案终极版,一个极低成本的 Android 屏幕适配方案).

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

如何将旧项目从 AndroidAutoLayout 迁移至 AndroidAutoSize (AndroidAutoSize 已被 鸿洋 推荐为 AndroidAutoLayout 的官方替换方案)

JessYanCoding opened this issue · comments

由于 AndroidAutoLayout 已经停止维护, AndroidAutoSize 作为 AndroidAutoLayout 官方推荐的屏幕适配框架,所以为大家准备了详细的 迁移指南,方便大家将自己的项目快速从 AndroidAutoLayout 替换为 AndroidAutoSize

在迁移之前还是建议先看下框架的 原理分析,更加理解框架的原理,才可以更快地上手 AndroidAutoSize,当遇到问题时也不至于手足无措

在旧项目的迁移时这个 issues 有可能会帮助到你。

迁移分为三个步骤:

  1. appbuild.gradle 中将 AndroidAutoLayout 的依赖替换为 AndroidAutoSize
dependencies {
-     implementation 'com.zhy:autolayout:1.4.5'
+     implementation 'me.jessyan:autosize:x.y.z'
}
  1. appAndroidManifest.xml 中将 AndroidAutoLayout 填写设计图尺寸的方式改为 AndroidAutoSize 的方式
<application>
-     <meta-data
-         android:name="design_width"
-         android:value="1080"/>
-     <meta-data
-         android:name="design_height"
-         android:value="1920"/>
+     <meta-data
+         android:name="design_width_in_dp"
+         android:value="360"/>
+     <meta-data
+         android:name="design_height_in_dp"
+         android:value="640"/>

</application>
  • 至于设计图尺寸是怎么算的我这里就不多说了,我的文章讲的很清楚,不懂就我的文章和 demo,如果使用副单位可以直接使用 1080 * 1920,不需要把设计图尺寸转换为 360 * 640,详情请看 demo-subunits
  1. 检查项目中所有的 xml 文件,将项目中所有的 AutoLayout 控件还原为原控件,并将 xml 文件中填写的 px 尺寸全部替换为 dp 尺寸,如果使用的是副单位,就替换为副单位
- <com.jess.arms.widget.autolayout.AutoCardView xmlns:android="http://schemas.android.com/apk/res/android"
+ <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
                                                     xmlns:tools="http://schemas.android.com/tools"
                                                     android:layout_width="match_parent"
-                                                    android:layout_height="550px"
-                                                    android:layout_marginRight="15px"
-                                                    android:layout_marginBottom="15px"
+                                                    android:layout_height="174dp"
+                                                    android:layout_marginRight="4dp"
+                                                    android:layout_marginBottom="4dp">


- </com.jess.arms.widget.autolayout.AutoCardView> 
+ </android.support.v7.widget.CardView> 

前两个步骤都比较简单,最后一个步骤,工作量比较大,所以可以在进行新业务开发的同时,抽时间慢慢完成旧代码的迁移工作

可以参照 MVPArms 迁移过程中的这两个 Commit删除 AndroidAutoLayout使用 AndroidAutoSize