java 无法隐藏底部工作表,Android

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42959780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 06:59:05  来源:igfitidea点击:

Can't hide Bottom Sheet, Android

javaandroidbottom-sheet

提问by alb

I'm having problems with my bottom-sheetbecause when I open the activity it is on, blocking the view enter image description here

我的底部工作表有问题,因为当我打开活动时,它会挡住视图 在此处输入图片说明

This happens, I think, because of the XML attribute declaring the bottom-sheetwith 350dp of height:

我认为发生这种情况是因为 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">

The thing is, I can't change that value to 0dp because the next time when I try to open the bottom-sheet, there is no bottom-sheet, because the height is 0dp, so it won't show anything. My question is, is there a way to declare the bottom-sheetoff? (I've tried to setState to STATE_COLLAPSED but didn't work). Bellow is the java code that interacts with the Bottom Sheet. JAVA:

问题是,我无法将该值更改为 0dp,因为下次当我尝试打开bottom-sheet 时,没有bottom-sheet,因为高度是 0dp,所以它不会显示任何内容。我的问题是,有没有办法声明底部工作表?(我试图将 setState 设置为 STATE_COLLAPSED 但没有奏效)。Bellow 是与Bottom Sheet 交互的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) {

            }
        });

回答by Pietro Scarampella

first you have to add the attribute

首先你必须添加属性

app:behavior_hideable="true"

in your

在你的

<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">

And then you can hide the bottom sheet using

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

mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN)

and not

并不是

mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED)

the state COLLAPSED is between HIDDEN and EXPANDED and his heigth must be specified by the attribute:

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

app:behavior_peekHeight="200dp"

回答by ALBPT

Write this:

写这个:

    mBottomSheetBehavior.setPeekHeight(0);

回答by Marius Kohmann

In my case i was not able to hide the bottomsheet and it was placed on top of my view. I found out that animateLayoutChanges = "true"in my layout file was causing this issue.

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

回答by Kishore Reddy

Inside onCreateadd these lines, it can hide the bottombar

在里面onCreate添加这些行,它可以隐藏底栏

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

回答by RoCk RoCk

In my case I was using BottomSheetDialog.

就我而言,我使用的是BottomSheetDialog.

app:behavior_hideable- attribute is used to determine if our bottom sheet will hide when it is swiped down. In other words bottom sheet top be off screen, if the peek height isn't set.

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

app:behavior_peekHeight- attribute value used to represent how much pixels the bottom sheet will be visible.

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>

I set the peekHeight to 50dp. And peek height has nothing to do with the bottomSheet layout height itself which I set 200dp (for example only).

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

peek

窥视

You can view the changes in your XML viewer if the bottom sheet is expanded, if so add the app:behavior_peekHeight = 0dpfrom the xml layout and it will hide and also inform you of the current state.

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

回答by Vicente Domingos

When Collapsed set app:behavior_hideable="false"

折叠时设置 app:behavior_hideable="false"

回答by Dany T Mpofu

You can manually hide that bottom sheet by setting the visibility of the parent linear layout to gone put this line in your code when you want

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

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); }

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); }

it worked for me please try it

它对我有用,请试试