BottomSheetBehaviour:视图与BottomSheetBehavior无关

Har*_*rma 17 android android-design-library androiddesignsupport bottom-sheet

我正在使用Google Design Support Library 25.0.0在我的活动中,我有一个相对布局

app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
Run Code Online (Sandbox Code Playgroud)

现在当我引用它来添加BottomSheetBehaviour时,我收到错误

java.lang.IllegalArgumentException: The view is not associated with BottomSheetBehavior
Run Code Online (Sandbox Code Playgroud)

这是布局:

<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>
Run Code Online (Sandbox Code Playgroud)

这是活动相关代码:

CoordinatorLayout colayout = (CoordinatorLayout) findViewById(R.id.maps_colayout);
    View bottomSheet = colayout.findViewById(R.id.rl_bottomsheet);
    BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);
    behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
        @Override
        public void onStateChanged(@NonNull View bottomSheet, int newState) {
            // React to state change
        }

        @Override
        public void onSlide(@NonNull View bottomSheet, float slideOffset) {
            // React to dragging events
        }
    });
Run Code Online (Sandbox Code Playgroud)

小智 18

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

app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
Run Code Online (Sandbox Code Playgroud)

  • 这解决了它,只是注意,因为他们迁移到AndroidX包所需的名称是:`app:layout_behavior ="com.google.android.material.bottomsheet.BottomSheetBehavior"` (13认同)

Mar*_*nak 9

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

xmlns:app="http://schemas.android.com/tools"
Run Code Online (Sandbox Code Playgroud)

xmlns:app="http://schemas.android.com/apk/res-auto"
Run Code Online (Sandbox Code Playgroud)

在布局文件中的CoordinatorLayout声明中。

tools命名空间是用来为“工具”(例如IDE)提供有关布局的附加信息。该信息已从应用中删除。

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

那么,tools名称空间实际上有什么用呢?最常见的用法是更好地渲染布局的预览。例如,假设您有一个TextView,它最初应该为空,以后再填写。

您可以将属性添加到TextView声明中:

tools:text="Some example text here"
Run Code Online (Sandbox Code Playgroud)

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


Lui*_*eno 6

我希望帮助其他人...我有这样的 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>
Run Code Online (Sandbox Code Playgroud)

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

<include
layout="@layout/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
Run Code Online (Sandbox Code Playgroud)

但这行不通。我遇到了同样的错误:“该视图与 BottomSheetBehavior 无关”

我将行为属性放在“包含”中,而不是放在底部,它起作用了:

<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"/>
Run Code Online (Sandbox Code Playgroud)