【问题标题】:BottomSheetBehavior not in androidX librariesBottomSheetBehavior 不在 androidX 库中
【发布时间】:2019-01-08 01:47:36
【问题描述】:

我将BottomSheetBehavior 与原始支持库一起使用:

implementation 'com.android.support:design:27.1.1' 

当我迁移到使用新的 androidx 库时,尽管缺少 BottomSheetBehavior。上述支持库中的映射也不在AndroidX Refactoring List 中,但迁移工具已将其删除。

在新的 androidx 库中包含 BottomSheetBehavior 我缺少什么。

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.google.android.material:material:1.0.0-beta01'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

    // ReactiveX
    implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
    implementation 'io.reactivex.rxjava2:rxkotlin:2.2.0'

    implementation 'com.android.support:design:28.1.0'

    // Android Compatability Libraries
    // Version: https://developer.android.com/topic/libraries/support-library/refactor
    implementation 'androidx.appcompat:appcompat:1.0.0-beta01'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha1'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0-beta01'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0-beta01'
    implementation 'androidx.recyclerview:recyclerview:1.0.0-beta01'

    // Android Navigation Component
    // Check here for updated version info - will move to androidx soon.
    // https://developer.android.com/topic/libraries/architecture/adding-components
    def nav_version = "1.0.0-alpha04"

    // use -ktx for Kotlin
    implementation "android.arch.navigation:navigation-fragment-ktx:$nav_version"
    implementation "android.arch.navigation:navigation-ui-ktx:$nav_version"
    androidTestImplementation "android.arch.navigation:navigation-testing-ktx:$nav_version"

    // Testing
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'
}

【问题讨论】:

    标签: android androidx bottom-sheet material-components-android


    【解决方案1】:

    事实证明,Android Studio Refactor > Migrate to AndroidX 中的重构工具没有正确迁移 BottomSheetBehaviour 的 XML。

    旧位置是android.support.design.widget.BottomSheetBehavior,并且没有被迁移工具修改。原来的 XML 是:

    <fragment
        android:id="@+id/player_bottom_sheet_fragment"
        android:name="app.rxsongbrowsertrials.ui.player.PlayerToggleFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:behavior_hideable="false"
        app:behavior_peekHeight="56dp"
        app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
        />
    

    新的位置是com.google.android.material.bottomsheet.BottomSheetBehavior,所以布局需要变成:

    <fragment
        android:id="@+id/player_bottom_sheet_fragment"
        android:name="app.rxsongbrowsertrials.ui.player.PlayerToggleFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:behavior_hideable="false"
        app:behavior_peekHeight="56dp"
        app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
        />
    

    【讨论】:

    • 我花了一整天的时间在这个上。希望这能让人们更容易发现。
    • 在来自 AS 的最后一次更新中,仍然没有更正 AndroidX 迁移中的这个错误。谢谢
    【解决方案2】:

    你也可以替换

        app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
    or 
        app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
    

    由

    app:layout_behavior="@string/bottom_sheet_behavior"
    

    【讨论】:

    • 我的项目是从 Android Studio 模板生成的,没有@string/bottom_sheet_behavior。我想我可以通过将implementation "com.google.android.material:material:1.1.0-alpha04" 添加到我的app/build.gradle 来实现它
    【解决方案3】:

    你必须导入谷歌提供的Material Components Library。

    Material Components for Android 是 Android 设计支持库的直接替代品。

    添加您的build.gradle:

    implementation 'com.google.android.material:material:x.x.x'
    

    然后使用类com.google.android.material.bottomsheet.BottomSheetBehavior。

    在你的布局中你可以使用属性:

        app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
        ..>
    

    或

    app:layout_behavior="@string/bottom_sheet_behavior"
    

    【讨论】:

      【解决方案4】:

      我收到此错误消息:

      Didn't find class "com.google.android.material.bottomsheet.BottomSheetBehaviour"
      

      解决这个问题的唯一方法是更改​​ XML:

      Change:
      
      app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
      
      Change to:
      
      app:layout_behavior="@string/bottom_sheet_behavior"
      

      这样就解决了问题

      【讨论】:

        猜你喜欢
        • 2019-05-05
        • 1970-01-01
        • 2017-09-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-02-24
        • 2017-04-03
        • 1970-01-01
        相关资源
        最近更新 更多