AndroidX中的持久性底表

时间:2019-12-11 05:59:05

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

  

layout_behavior-    android.support.design.widget.BottomSheetBehavior 无法正常工作

     

此视图在运行时不受垂直限制,它将跳转

我仅在Androidx中收到此错误。因此,我该如何解决此错误。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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"
    android:background="#efefef"
    tools:context=".MainActivity">


    <include layout="@layout/content_main" />

    <!-- Adding bottom sheet after main content -->
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/bottom_sheet"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#fff"
        android:orientation="vertical"
        android:padding="10dp"
        app:behavior_hideable="true"
        app:behavior_peekHeight="56dp"
        app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

        ...... child layouts..


    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

enter image description here

4 个答案:

答案 0 :(得分:1)

由于您使用的是androidx库,因此必须使用Material Components Library
使用类com.google.android.material.bottomsheet.BottomSheetBehavior代替android.support.design.widget.BottomSheetBehavior

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

或:

   <LinearLayout     
      app:layout_behavior="@string/bottom_sheet_behavior"
    ..>

也将ConstraintLayout更改为CoordinatorLayout

答案 1 :(得分:0)

尝试将这些属性添加到线性布局和其他组件中:

app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"

因此您的线性布局应如下所示:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/bottom_sheet"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#fff"
            android:orientation="vertical"
            android:padding="10dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:behavior_hideable="true"
            app:behavior_peekHeight="56dp"
            app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

,还将这三个属性添加到约束布局的所有直接子布局中。

答案 2 :(得分:0)

您可以尝试垂直限制LinearLayout吗?

app:layout_constraintBottom_toBottomOf="parent"

答案 3 :(得分:0)

通过替换解决错误

androidx.constraintlayout.widget.ConstraintLayout 

androidx.coordinatorlayout.widget.CoordinatorLayout 

app:layout_behavior="android.support.design.widget.BottomSheetBehavior"

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

在activity_main.xml中

正确的代码是

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
    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"
    android:background="#efefef"
    tools:context=".MainActivity">


    <include layout="@layout/content_main" />

    <!-- Adding bottom sheet after main content -->
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/bottom_sheet"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#fff"
        android:orientation="vertical"
        android:padding="10dp"
        app:behavior_hideable="true"
        app:behavior_peekHeight="56dp"
        app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">

        ...... child layouts..


    </LinearLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>