简体   繁体   English

Android 底部工作表模式(对话框)未完全打开

[英]Android Bottom Sheet Modal (Dialog) doesn't open completely

I am trying to show a bottom sheet dialog in my app on a button click.我试图在单击按钮时在我的应用程序中显示底部工作表对话框。 But the dialog is opening partially.但是对话框正在部分打开。 I would like to open the dialog completely on button click.我想在单击按钮时完全打开对话框。

I have tried following code.我试过以下代码。

MainActivity.java主活动.java

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    View showModalBottomSheet = findViewById(R.id.as_modal);
    showModalBottomSheet.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Initializing a bottom sheet
            BottomSheetDialogFragment bottomSheetDialogFragment = new CustomBottomSheetDialogFragment();

            //show it
            bottomSheetDialogFragment.show(getSupportFragmentManager(), bottomSheetDialogFragment.getTag());
        }
    });
}
}

CustomBottomSheetDialogFragment.java CustomBottomSheetDialogFragment.java

public class CustomBottomSheetDialogFragment extends BottomSheetDialogFragment {


@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

private BottomSheetBehavior.BottomSheetCallback mBottomSheetBehaviorCallback = new BottomSheetBehavior.BottomSheetCallback() {

    @Override
    public void onStateChanged(@NonNull View bottomSheet, int newState) {
        if (newState == BottomSheetBehavior.STATE_HIDDEN) {
            dismiss();
        }
    }

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

@Override
public void setupDialog(Dialog dialog, int style) {
    super.setupDialog(dialog, style);
    View contentView = View.inflate(getContext(), R.layout.dialog_modal, null);
    dialog.setContentView(contentView);
    CoordinatorLayout.LayoutParams layoutParams =
            (CoordinatorLayout.LayoutParams) ((View) contentView.getParent()).getLayoutParams();
    CoordinatorLayout.Behavior behavior = layoutParams.getBehavior();
    if (behavior != null && behavior instanceof BottomSheetBehavior) {
        ((BottomSheetBehavior) behavior).setBottomSheetCallback(mBottomSheetBehaviorCallback);
    }
}
}

activity_main.xml活动_main.xml

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

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

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

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <Button
        android:id="@+id/as_modal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/activity_horizontal_margin"
        android:text="@string/modal" />

</android.support.v4.widget.NestedScrollView>

Here you can find the link to the project:您可以在此处找到该项目的链接:

Github Project Link Github 项目链接

Current Behaviour:当前行为:

当前行为的屏幕截图

At this lines in the setUpDialog method can solve the problem在 setUpDialog 方法中的这一行可以解决问题

 BottomSheetDialog d = (BottomSheetDialog) dialog;
 FrameLayout bottomSheet = (FrameLayout) d.findViewById(android.support.design.R.id.design_bottom_sheet);
 BottomSheetBehavior.from(bottomSheet).setState(BottomSheetBehavior.STATE_EXPANDED);

That method write in your CustomBottomSheetDialogFragment in onCreate method该方法在 onCreate 方法中写入您的 CustomBottomSheetDialogFragment

getDialog().setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            BottomSheetDialog d = (BottomSheetDialog) dialog;
            FrameLayout bottomSheet =  d.findViewById(R.id.design_bottom_sheet);
            CoordinatorLayout lyout = (CoordinatorLayout) bottomSheet.getParent();
            BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);
            behavior.setPeekHeight(bottomSheet.getHeight());
            lyout.getParent().requestLayout();
        }
    });

The BottomSheetDialogFragment and BottomSheetDialog in Android Support Library Vesion 23.2.0 had some issues. Android 支持库版本 23.2.0 中的BottomSheetDialogFragmentBottomSheetDialog存在一些问题。

You can check this doc in Android Support Library, revision 23.2.1 (March 2016) section.您可以在 Android 支持库,修订版 23.2.1(2016 年 3 月)部分中查看此文档

So, the solution is to update your com.android.support:design version to above 23.2.0.因此,解决方案是将您的com.android.support:design版本更新到 23.2.0 以上。 (23.2.1、23.3.0、23.4.0 whatever the new version). (23.2.1、23.3.0、23.4.0无论是新版本)。

I have tested your code in the new version.我已经在新版本中测试了您的代码。 It worked normally.它正常工作。

Hope it help.希望有帮助。

你应该为你的行为设置peekHeight

I have followed these steps, this helped me for BottomSheetDialog dialog .我遵循了这些步骤,这对我的BottomSheetDialog dialog帮助。

Step1: To create a BottomSheetBehaviour , you need the FrameLayout of the dialog which exists by default. Step1:要创建一个BottomSheetBehaviour ,您需要默认存在的对话框的FrameLayout

FrameLayout bottomSheet = dialog.findViewById(android.support.design.R.id.design_bottom_sheet);

if you are using androidx, use如果您使用的是 androidx,请使用

FrameLayout bottomSheet = dialog.findViewById(com.google.android.material.R.id.design_bottom_sheet);

Step2: Set bottom sheet behaviour to Expanded state and also set a peek height步骤 2:将底部工作表行为设置为展开状态并设置查看高度

BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);
behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
behavior.setPeekHeight(screenHeight / 2);
behavior.setHideable(false);

Step3: Don't allow dialog to cancel on touch outside步骤 3:不允许对话框在外部触摸时取消

dialog.setCanceledOnTouchOutside(false);

Note: You can calculate screenHeght dynamically using DisplayMetrics.注意:您可以使用 DisplayMetrics 动态计算 screenHeght。

DisplayMetrics screenMetrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(screenMetrics);
            int screenHeight = screenMetrics.heightPixels;
            int screenWidth = screenMetrics.widthPixels;

add something like this to onCreateView method:将这样的内容添加到 onCreateView 方法中:

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    getDialog().setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            BottomSheetDialog d = (BottomSheetDialog) dialog;
            View bottomSheetInternal = d.findViewById(android.support.design.R.id.design_bottom_sheet);
            BottomSheetBehavior.from(bottomSheetInternal).setState(BottomSheetBehavior.STATE_EXPANDED);
        }
    });
    return inflater.inflate(R.layout.your_bottomsheet_content_layout, container, false);
}

for AndroidX use供 AndroidX 使用

com.google.android.material.R.id.design_bottom_sheet

instead of而不是

android.support.design.R.id.design_bottom_sheet

Do not use fragment use BottomSheetDialog.不要使用片段使用BottomSheetDialog。 Sample demo uploaded to https://github.com/bita147/BottomSheetDialog示例演示上传到https://github.com/bita147/BottomSheetDialog

For Model dialog just set对于模型对话框刚刚设置

bottomshetDialod.setCanceledOnTouchOutside(false);

Works for me.为我工作。

Same thing was happening with me.同样的事情也发生在我身上。 The weird reason behind it was Toolbar If you remove the toolbar then bottomsheet will be shown on full screen.其背后的奇怪原因是Toolbar如果您删除工具栏,则底部表格将全屏显示。

I don't know the reason behind it.我不知道背后的原因。 But after removing Toolbar it was working fine.但是删除工具栏后它工作正常。 You can try that.你可以试试。

This code works for me.这段代码对我有用。

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {

    dialog!!.setOnShowListener { dialog ->
        val d = dialog as BottomSheetDialog
        val bottomSheetInternal: FrameLayout? =
            d.findViewById(com.google.android.material.R.id.design_bottom_sheet)
        BottomSheetBehavior.from(bottomSheetInternal!!).state =
            BottomSheetBehavior.STATE_EXPANDED
    }
    val view = inflater.inflate(R.layout.bottom_sheet_modal, container, false)

    return view
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM