我正在使用Google Design Support Library 25.0.0 在我的活动中,我有一个相对布局
app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
现在当我引用它来添加BottomSheetBehaviour时,我收到了错误
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>
这是与活动相关的代码:
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
}
});
答案 0 :(得分:13)
您需要在线性布局标记中添加此行。被引用的那个。
app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
答案 1 :(得分:6)
您声明了app
名称空间错误。替换行:
xmlns:app="http://schemas.android.com/tools"
与
xmlns:app="http://schemas.android.com/apk/res-auto"
在布局文件中的CoordinatorLayout声明中。
tools
命名空间用于向“工具”(例如IDE)提供有关布局的其他信息。此信息将从应用中删除。
另一方面,app
命名空间是所有自定义(即未由Android系统声明)属性的全局命名空间,由您或导入的库声明(设计支持库是您的案件)。这些都包含在您的应用中。
那么实际上tools
命名空间的优点是什么?最常见的用法是更好地渲染布局的预览。例如,假设您有一个TextView,它应该最初为空并稍后填充。
您可以在TextView声明中添加属性:
tools:text="Some example text here"
此文字不会显示在您的应用中。但是,在Android Studio中呈现的布局预览会有它,因此您可以看到移动设备上的内容。
答案 2 :(得分:1)
首先,如果使用的是androidX,则选择“ app:layout_behavior="@string/bottom_sheet_behavior"
”或“ app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
第二,具有底页行为的视图必须是协调器布局的直接子元素。
最后,如果需要的话,删除该元素的layout_width
和layout_height
属性。
答案 3 :(得分:0)
我希望可以帮助别人... 我有这样的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"
/>
但这不起作用。我遇到了同样的错误:“视图与BottomSheetBehavior没有关联”
我将行为属性放在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"/>