BottomSheetBehavior不是CoordinatorLayout的子级

问题描述 投票:11回答:3

这是我的XML布局,名称为歌曲列表:

enter image description here

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/viewA"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="0.6"
            android:background="@android:color/holo_purple"
            android:orientation="horizontal"/>

        <android.support.v4.widget.NestedScrollView
            android:id="@+id/bottom_sheet"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/holo_blue_bright"
            app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
            >
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <ListView
                    android:id="@+id/list"
                    android:layout_width="match_parent"
                    android:layout_height="308dp"
                    />
            </LinearLayout>

        </android.support.v4.widget.NestedScrollView>
    </LinearLayout>
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:clickable="true"
        android:src="@drawable/personlog"
        app:layout_anchor="@id/viewA"
        app:layout_anchorGravity="bottom|center"/>

</android.support.design.widget.CoordinatorLayout>

这是我的片段,其中包含此布局:

public class SongList extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.songlist,container,false);

        textView=(TextView)view.findViewById(R.id.txt);

        View bottomSheet = view.findViewById(R.id.bottom_sheet);
        BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
        bottomSheetBehavior.setPeekHeight(200);
return view;}
}

但是午饭时应用程序给我这个错误:

java.lang.IllegalArgumentException: The view is not a child of CoordinatorLayout

从此行:

  BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);

如何解决?似乎一切正常,但会给出该错误...如果有人可以请帮助

android android-coordinatorlayout
3个回答
11
投票

BottomSheetBehavior

用于CoordinatorLayout的子视图的交互行为插件,使其可以用作底页。

目前您的底页BottomSheetBehaviorNestedScrollView的子级。因此,只需完全丢弃最外面的LinearLayout

LinearLayout

但是现在您尝试实现的底表还有更多问题。首先,您不应该在滚动视图中使用<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:id="@+id/viewA" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="0.6" android:background="@android:color/holo_purple" android:orientation="horizontal"/> <android.support.v4.widget.NestedScrollView android:id="@+id/bottom_sheet" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/holo_blue_bright" app:layout_behavior="android.support.design.widget.BottomSheetBehavior"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="308dp" /> </LinearLayout> </android.support.v4.widget.NestedScrollView> <android.support.design.widget.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="16dp" android:clickable="true" android:src="@drawable/personlog" app:layout_anchor="@id/viewA" app:layout_anchorGravity="bottom|center" /> </android.support.design.widget.CoordinatorLayout> 。其次,您不应在滚动视图中使用列表视图,因为它正在实现自己的滚动。您可以仅通过将列表视图用作底部页面来简化此操作。


0
投票

就我而言,而不是

wrap_content

我使用了<?xml version="1.0" encoding="utf-8"?> <android.support.design.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" > (在包含布局和BottomSheet的片段或活动中。)>


0
投票

[注意,如果您不使用CoordinatorLayout而是使用BottomSheetDialogFragment(可以为您创建一个),我注意到您无法使用Nav组件库的当前版本(2.1。 0-alpha05),并且必须将其实例化为新的片段对话框,否则会出现此错误,即不要使用此错误:

© www.soinside.com 2019 - 2024. All rights reserved.