https://github.com/JessYanCoding/AndroidAutoSize
https://www.wanandroid.com/blog/show/2343
https://www.jianshu.com/p/12ce5c0a6163
implementation 'me.jessyan:autosize:1.2.1'
<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>
1080X1920分辨率 480dpi 360dpX640dp
1080/360=3
dp = px / (DPI / 160)
width=360dp=1080px/(480dpi/160)
适配前
适配后
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoSizeTextType="uniform"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/textView2"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints" />
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintEnd_toStartOf="@+id/textView3"
app:layout_constraintStart_toStartOf="@+id/textView3"
app:layout_constraintTop_toTopOf="@+id/textView3"
tools:ignore="MissingConstraints">
<TextView
android:layout_width="180dp"
android:layout_height="180dp"
android:background="@color/purple_200" />
<TextView
android:layout_width="180dp"
android:layout_height="50dp"
android:background="@color/purple_500" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintStart_toStartOf="@+id/linearLayout"
app:layout_constraintTop_toBottomOf="@+id/linearLayout"
tools:ignore="MissingConstraints">
<Button
android:layout_width="180dp"
android:layout_height="100dp"
android:background="#ccc"
android:text="stop"
android:textColor="#fff"
android:textSize="50sp" />
<Button
android:layout_width="180dp"
android:layout_height="100dp"
android:text="restart"
android:textColor="#fff"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout3"
android:layout_width="0dp"
android:layout_height="40dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linearLayout2"
tools:ignore="MissingConstraints">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/purple_700"
app:layout_constraintTop_toBottomOf="@+id/linearLayout2"
tools:ignore="MissingConstraints" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/purple_200"
app:layout_constraintTop_toBottomOf="@+id/linearLayout2"
tools:ignore="MissingConstraints" />
</LinearLayout>
<TextView
android:id="@+id/textView3"
android:layout_width="1dp"
android:layout_height="0dp"
android:background="#000"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints" />
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="1dp"
android:background="#000"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints" />
<TextView
android:id="@+id/textView4"
android:layout_width="120dp"
android:layout_height="120dp"
android:background="@color/purple_200"
app:layout_constraintTop_toBottomOf="@+id/linearLayout3"
tools:ignore="MissingConstraints" />
<TextView
android:id="@+id/textView5"
android:layout_width="120dp"
android:layout_height="120dp"
android:background="@color/purple_500"
app:layout_constraintStart_toEndOf="@+id/textView4"
app:layout_constraintTop_toBottomOf="@+id/textView4"
tools:ignore="MissingConstraints" />
<TextView
android:layout_width="120dp"
android:layout_height="120dp"
android:background="@color/purple_700"
app:layout_constraintStart_toEndOf="@+id/textView5"
app:layout_constraintTop_toBottomOf="@+id/textView5"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
使用副单位开发
AutoSizeConfig.getInstance().getUnitsManager()
.setSupportDP(false)
.setSupportSP(false)
.setSupportSubunits(Subunits.MM);
fragment支持
AutoSizeConfig.getInstance().setCustomFragment(true);