"The first line of code" version 2, the percentage layout has been deprecated, which is a big headache.
The solution is to add a sentence of androidx.percent... under build.gradle, see below, and then sync:
dependencies {
implementation 'androidx.appcompat:appcompat:1.4.2'
implementation 'com.google.android.material:material:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.percentlayout:percentlayout:1.0.0'
...
}
Then the beginning layout in xml is also changed, but it can still be seen that it is crossed:
<?xml version="1.0" encoding="utf-8"?>
<androidx.percentlayout.widget.PercentFrameLayout 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=".FourthActivity">
<Button
android:id="@+id/button_1"
app:layout_widthPercent="50%"
app:layout_heightPercent="50%"
android:layout_gravity="left|top"
android:text="button1"
android:textAllCaps="false"/>
<Button
android:id="@+id/button_2"
app:layout_widthPercent="50%"
app:layout_heightPercent="50%"
android:layout_gravity="right|top"
android:text="button2"
android:textAllCaps="false"/>
<Button
android:id="@+id/button_3"
app:layout_widthPercent="50%"
app:layout_heightPercent="50%"
android:layout_gravity="left|bottom"
android:text="button3"
android:textAllCaps="false"/>
<Button
android:id="@+id/button_4"
app:layout_widthPercent="50%"
app:layout_heightPercent="50%"
android:layout_gravity="right|bottom"
android:text="button4"
android:textAllCaps="false"/>
</androidx.percentlayout.widget.PercentFrameLayout>
Then it can be run, the effect is as follows: