BottomSheetBehavior不在androidX库中。

102

我曾经使用过原始的支持库 BottomSheetBehavior:

implementation 'com.android.support:design:27.1.1' 
当我迁移到使用新的androidx库时,发现BottomSheetBehavior缺失了。上述的支持库映射也未包含在AndroidX重构列表中,但迁移工具已将其删除。
我需要做什么才能在新的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'
}
5个回答

235

原来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"
    />

8
我为这个问题花了一整天的时间。希望这样可以让更多人更轻松地发现它。 - AdamHurwitz
在最新的AS更新中,AndroidX迁移中仍未修正此错误。谢谢。 - Genaut

59
你也可以替换

    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"

1
我的项目是从Android Studio模板生成的,但是没有@string/bottom_sheet_behavior。我认为通过在app/build.gradle中添加implementation "com.google.android.material:material:1.1.0-alpha04",我成功地引入了它。 - Michael Osofsky

27

您需要导入由Google提供的Material Components库

Android的Material Components是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"

2

对于 R 类

com.google.android.material.R.id.design_bottom_sheet

替代

android.support.design.R.id.design_bottom_sheet

1
我收到了这个错误信息:
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"

这个问题已经解决了。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接