Version 28 (intended for Android Pie and below) is the last version of the legacy support library


在學習《第一行代碼:Android篇》時,做書中的Demo,案例是:
打開app/build.gradle文件,在dependencies閉包中添加如下內容:

dependencies{
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:24.2.1'
    compile 'com.android.support:percent:24.2.1'
    testCompile 'junit:junit:4.12'
}

此時,Android Studio已經幫助檢查出是過時了:

image

經過上網查閱,找到報錯原因:

  1. 由於Android Studio 版本較高,添加庫依賴已經不支持compile語句,應使用implementation或者api
  2. 若使用api或implementation語句仍然報錯,可能是庫的版本較低,出現了不兼容的現象
  3. 由於原來的support 庫太亂了,谷歌在新版本中取消了support庫,使用了新的andriodX庫

解決方法:
需要將原理的support庫遷移到AndroidX並使用implementation添加依賴。

步驟一:將dependenci閉包修改如下

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.0'
    implementation 'androidx.percentlayout:percentlayout:1.0.0'
    testImplementation 'junit:junit:4.13.2'
}

步驟二:遷移到 AndroidX

image

image

image

測試,Launch succeeded ,當下問題解決了。

image

但是,在做下一個Demo的時候,又出現了問題:原文在修改activity_main.xml中的代碼,使用android.support.percent.PercentFrameLayout的時候,我這里爆紅!

image

錯誤信息是:Cannot resolve class android.support.percent.PercentFrameLayout

網上查閱資料是,原因有以下幾點:

  1. AS已經不推薦使用compile導入依賴庫
  2. 由於引入的第三方依賴庫依賴了androidx ,而自己本身的庫又是依賴的support ,導致兩者不能共存
  3. 書中介紹的百分比布局PercentFrameLayout和PercentRelativeLayout已經廢棄,被androidx.percentlayout:percentlayout:1.0.0替代

解決方法:

  1. 將dependenci閉包修改如下:(上面的dependencies徹底更換)
dependencies {
    implementation 'androidx.appcompat:appcompat:1.3.0'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    testImplementation 'junit:junit:4.13.2'
    implementation 'androidx.percentlayout:percentlayout:1.0.0'
}
  1. 點擊sync now,安裝依賴庫

image

  1. 將布局文件(activity_main.xml)中的標簽改為androidx.percentlayout.widget.PercentFrameLayout
<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button1"
        android:text="Button1"
        android:layout_gravity="right|top"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="50%"/>

    <Button
        android:id="@+id/button2"
        android:text="Button2"
        android:layout_gravity="left|bottom"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="50%"/>

    <Button
        android:id="@+id/button3"
        android:text="Button3"
        android:layout_gravity="right|bottom"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="50%"/>

    <Button
        android:id="@+id/button4"
        android:text="Button4"
        android:layout_gravity="left|top"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="50%"/>


</androidx.percentlayout.widget.PercentFrameLayout>
  1. 此時,上面代碼,Android Stdio會報錯:

image

這是因為老版本的Android Studio中內置了布局的檢查機制,認為每一個控件都應該通過android:layout_widthandroid:layout_height屬性指定寬高才是合法的。而其實我們是通過app:layout_widthPercent和app:layout_heightPercent屬性來指定寬高的,所以Android Studio沒檢測到。這個錯誤提示並不影響程序運行,忽視,直接運行即可。

image

如果實在有強迫症,不想讓activity_main.xml中的Button爆紅,可設置一下android:layout_heightandroid:layout_width這兩個屬性就好了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM