Android BottomSheetBehavior:该视图与BottomSheetBehavior不关联

Android BottomSheetBehavior:该视图与BottomSheetBehavior不关联,android,android-design-library,androiddesignsupport,bottom-sheet,Android,Android Design Library,Androiddesignsupport,Bottom Sheet,我正在使用谷歌设计支持库25.0.0 在我的活动中,我与 app:layout_behavior="android.support.design.widget.BottomSheetBehavior" 现在,当我引用它来添加BottomSheet行为时,我得到了错误 java.lang.IllegalArgumentException: The view is not associated with BottomSheetBehavior 布局如下: <android.support.

我正在使用谷歌设计支持库25.0.0 在我的活动中,我与

app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
现在,当我引用它来添加BottomSheet行为时,我得到了错误

java.lang.IllegalArgumentException: The view is not associated with BottomSheetBehavior
布局如下:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/maps_colayout"
xmlns:app="http://schemas.android.com/tools"
android:fitsSystemWindows="true"
android:background="@color/white">

...

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="280dp"
    android:layout_gravity="bottom"
    android:id="@+id/rl_bottomsheet"
    android:background="#F3F3F3"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

    ...

</RelativeLayout>

您声明了错误的
app
命名空间。替换行:

xmlns:app="http://schemas.android.com/tools"

在布局文件的CoordinatorLayout声明中

tools
名称空间用于向“tools”(例如IDE)提供有关布局的附加信息。此信息将从应用程序中删除

另一方面,
app
名称空间是所有自定义(即,未由Android系统声明)属性的全局名称空间,由您或导入的库(设计支持库是您的情况)声明。这些都包含在你的应用程序中

那么
工具
名称空间究竟有什么好处呢?最常见的用法是更好地渲染布局的预览。例如,假设您有一个TextView,它最初应该是空的,以后再填充

您可以向TextView声明添加属性:

tools:text="Some example text here"

此文本不会显示在你的应用程序中。但是,在Android Studio中呈现的布局预览将具有此功能,因此您可以查看移动设备上的预期内容。

您需要在线性布局标记中添加此行。被引用的那个

app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
或适用于androidx

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

首先,如果使用androidX,则“
app:layout\u behavior=“@string/bottom\u sheet\u behavior”
”或
app:layout\u behavior=“com.google.android.material.bottomsheet.BottomSheetBehavior”

其次,具有底部工作表行为的视图必须是协调器布局的直接子视图


最后,删除该元素的
layout\u width
layout\u height
属性(如果需要),前提是它是按编程方式执行的

我希望帮助别人。。。 我的mi bottom_sheet.xml如下所示:

<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="#ffffff"
    android:orientation="vertical"
    android:padding="16dp"
    app:behavior_hideable="false"
    app:behavior_peekHeight="90dp"
    app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
    
    .....
    
</LinearLayout>

.....
然后我将我的bottom_sheet.xml包含到我的activity.xml中,如下所示:

<include
layout="@layout/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>

但这是行不通的。我也犯了同样的错误:“视图与行为无关”

我将行为属性放在Include中,而不是放在botton中,它成功了:

<include
            layout="@layout/bottom_sheet"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:behavior_hideable="false"
            app:behavior_peekHeight="90dp"
            app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"/>


这很有效。我怎么知道什么时候使用/tools或/apk/res auto这帮我解决了问题,只需注意,因为它们迁移到了AndroidX软件包,所以需要的名称是:
app:layout\u behavior=“com.google.android.material.bottomsheet.BottomSheetBehavior”
谢谢,最后一部分救了我。
<include
layout="@layout/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<include
            layout="@layout/bottom_sheet"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:behavior_hideable="false"
            app:behavior_peekHeight="90dp"
            app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"/>