无法隐藏底部表,Android

问题描述 投票:6回答:6

我的有问题,因为当我打开活动时它会打开,阻止视图enter image description here

我想这会发生,因为XML属性声明的高度为350dp:

<android.support.v4.widget.NestedScrollView
    android:id="@+id/bottom_sheet"
    android:layout_width="match_parent"
    android:layout_height="350dp"
    android:background="?android:attr/windowBackground"
    android:clipToPadding="true"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

问题是,我无法将该值更改为0dp,因为下次当我尝试打开时,没有,因为高度为0dp,所以它不会显示任何内容。我的问题是,有没有办法宣布关闭? (我尝试将stateState设置为STATE_COLLAPSED但不起作用)。 Bellow是与底部工作表交互的java代码。 JAVA:

View bottomSheet = findViewById( R.id.bottom_sheet );
        mBottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
        mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
        mBottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override
            public void onStateChanged(View bottomSheet, int newState) {
                if (newState == BottomSheetBehavior.STATE_COLLAPSED) {
                    //mBottomSheetBehavior.setPeekHeight(0);
                    //mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
                    //mBottomSheetBehavior.isHideable();
                }
            }

            @Override
            public void onSlide(View bottomSheet, float slideOffset) {

            }
        });
java android bottom-sheet
6个回答
9
投票

写这个:

    mBottomSheetBehavior.setPeekHeight(0);

6
投票

首先,您必须添加属性

app:behavior_hideable="true"

在你的

<android.support.v4.widget.NestedScrollView
    android:id="@+id/bottom_sheet"
    android:layout_width="match_parent"
    android:layout_height="350dp"
    android:background="?android:attr/windowBackground"
    android:clipToPadding="true"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

然后你可以使用隐藏底部工作表

mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN)

并不是

mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED)

状态COLLAPSED介于HIDDEN和EXPANDED之间,其高度必须由属性指定:

app:behavior_peekHeight="200dp"

1
投票

在我的情况下,我无法隐藏底片,它被放置在我的视图之上。我发现我的布局文件中的animateLayoutChanges = "true"导致了这个问题。


0
投票

在我的情况下,我使用BottomSheetDialog

app:behavior_hideable - 属性用于确定我们的底部工作表是否会在向下滑动时隐藏。换句话说,如果未设置峰值高度,则底部页面顶部不在屏幕上。

app:behavior_peekHeight - 用于表示底部工作表可见多少像素的属性值。

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/bottom_sheet_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="10dp"
android:orientation="vertical"
android:background="@color/colorPrimaryDerived"
app:layout_behavior="@string/bottom_sheet_behavior"
app:behavior_hideable="true"
app:behavior_peekHeight="0dp"> ........... </LinearLayout>

我将peekHeight设置为50dp。并且peek高度与我设置200dp的bottomSheet布局高度本身无关(仅作为示例)。

peek

如果底部工作表已展开,您可以在XML查看器中查看更改,如果是这样,则在xml布局中添加app:behavior_peekHeight = 0dpfrom,它将隐藏并通知您当前状态。


0
投票

onCreate里面添加这些线条,它可以隐藏底栏

mBottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
mBottomSheetBehavior.setHideable(true); //Important to add
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN); //Important to add

-1
投票

您可以通过设置父线性布局的可见性来手动隐藏底部工作表,以便在需要时将此行放在代码中

if (confirmLayoutBehaviour.getState() != BottomSheetBehavior.STATE_EXPANDED) { //todo hide your bottom sheet if its already open confirmLayout.setVisibility(View.GONE); } else { //set it to visible if its not open confirmLayout.setVisibility(View.VISIBLE); }

它对我有用,请尝试一下

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