Android百分比布局被弃用的问题

《第一行代码》版本2,那个百分比布局已经被弃用了,令人头大。
解决方法,在build.gradle下面添加一句androidx.percent…,看下面,然后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'
    ...
}

然后xml中的开头布局也改一下,但其实还是可以看出来它被划线了:

<?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>

然后可以运行了,效果如下:
1

猜你喜欢

转载自blog.csdn.net/qq_43738932/article/details/126040023
今日推荐