1.下载今日头条屏幕适配的依赖包
implementation 'me.jessyan:autosize:1.2.1'
2.在AndroidManifest.xml中配置参数,根据设计图的尺寸填写宽度和高度,一般写宽度就可以了。一般设计师是以375宽度进行设计
<manifest>
<application>
...
<meta-data
android:name="design_width_in_dp"
android:value="360"/>
<meta-data
android:name="design_height_in_dp"
android:value="640"/>
...
</application>
</manifest>
3.如果下载不下来,出现报错内容如下
Caused by: org.gradle.api.internal.artifacts.ivyservice.DefaultLenientConfiguration$ArtifactResolveException: Could not resolve all files for configuration ':app:debugRuntimeClasspath'.
Caused by: org.gradle.internal.resolve.ModuleVersionNotFoundException: Could not find me.jessyan:autosize:1.2.1.
那么可以这样修改。
如果构建项目时拉取不到gradle资源,可以将地址修改成阿里云的国内镜像。解决方式如下:
问题解决
打开build.gradle文件,加入代码
maven{ url 'https://maven.aliyun.com/repository/google'}
maven{ url 'https://maven.aliyun.com/repository/jcenter'}如下所示:
repositories {
//加入代码
maven{ url 'https://maven.aliyun.com/repository/google'}
maven{ url 'https://maven.aliyun.com/repository/jcenter'}
}
allprojects {
repositories {
//加入代码
maven{ url 'https://maven.aliyun.com/repository/google'}
maven{ url 'https://maven.aliyun.com/repository/jcenter'}
}
}
最后屏幕适配的效果如下图,这是360dp的宽度
这是适配的布局文件内容。如下代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.xf.ap.MainActivity2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView3"
android:layout_width="180dp"
android:layout_height="50dp"
android:background="@color/teal_200"
android:text="TextView" />
<TextView
android:id="@+id/textView4"
android:layout_width="180dp"
android:layout_height="50dp"
android:background="@color/purple_500"
android:text="TextView" />
</LinearLayout>
</RelativeLayout>