android:用AndroidAutoSize实现屏幕自适应
一,安装第三方库:
第三方库地址:
https://mvnrepository.com/artifact/me.jessyan/autosize
编辑build.gradle,添加:
// https://mvnrepository.com/artifact/me.jessyan/autosize
implementation 'me.jessyan:autosize:1.2.1'
然后点击Sync Now
二,代码:
编辑AndroidManifests.xml,在application段中添加:
<!-- 设计图宽度 -->
<meta-data
android:name="design_width_in_dp"
android:value="360"/>
<!-- 设计图高度 -->
<meta-data
android:name="design_height_in_dp"
android:value="640"/>
如果以确定宽度为主,可以只添加宽度,例如:
<!-- 设计图宽度 -->
<meta-data
android:name="design_width_in_dp"
android:value="640"/>
<!-- 设计图高度 -->
例子:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="100dp"
android:orientation="horizontal">
<TextView
android:layout_width="100dp"
android:layout_height="match_parent"
android:fontFamily="@font/fzfwzzay"
android:background="#ff0000"
android:text="100dp"
/>
<TextView
android:layout_width="200dp"
android:layout_height="match_parent"
android:fontFamily="@font/fzfwzzay"
android:background="#ffff00"
android:text="200dp"
/>
<TextView
android:layout_width="340dp"
android:layout_height="match_parent"
android:fontFamily="@font/fzfwzzay"
android:background="#ff00ff"
android:text="340dp"
/>
</LinearLayout>
三,测试效果:
设计图宽度为640时的效果:
设计图宽度为800时的效果: