BottomSheetBehavior不在androidX库中


89

BottomSheetBehavior在原始支持库中使用:

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

当我迁移使用新androidx库时,尽管BottomSheetBehavior缺少了。以上支持库中的映射也不在AndroidX重构列表中,但迁移工具已将其删除。

我缺少将BottomSheetBehavior包含在新androidx库中的功能。

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'
}

Answers:


222

事实证明,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"
    />

7
我整天都在这上面度过。希望这可以使人们更容易发现。
亚当·赫维兹

在AS的最新更新中,AndroidX迁移中的该错误仍未得到纠正。谢谢
Genaut

非常感谢
Sardorbek Rkh

52

您也可以更换

    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。我认为我可以通过添加implementation "com.google.android.material:material:1.1.0-alpha04"到我的app/build.gradle
Michael Osofsky '19

21

您必须导入Google提供的材料组件库

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"
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.