【问题标题】:Android Bottom Sheet Modal (Dialog) doesn't open completelyAndroid 底部工作表模式(对话框)未完全打开
【发布时间】:2016-10-17 13:25:50
【问题描述】:

我试图在单击按钮时在我的应用程序中显示底部工作表对话框。但是对话框正在部分打开。我想在单击按钮时完全打开对话框。

我已经尝试了以下代码。

MainActivity.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

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

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

您可以在这里找到项目的链接:

Github Project Link

当前行为:

【问题讨论】:

    标签: android android-layout material-design


    【解决方案1】:

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

    【讨论】:

    • 我不确定它为什么会这样工作,但我认为最好将其指向 STATE_COLLAPSED。
    • 只需将其设置为期望值:app:behavior_peekHeight="180dp"
    • 这对我有用,但是我必须使用 R.id.design_bottom_sheet 而不是 android.support.design.R.id.design_bottom_sheet
    【解决方案2】:

    该方法在 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();
            }
        });
    

    【讨论】:

      【解决方案3】:

      Android 支持库版本 23.2.0 中的 BottomSheetDialogFragmentBottomSheetDialog 存在一些问题。

      您可以在 Android 支持库,修订版 23.2.1(2016 年 3 月)部分查看此 doc

      因此,解决方案是将您的com.android.support:design 版本更新到 23.2.0 以上。 (23.2.1、23.3.0、23.4.0 无论新版本)。

      我已经在新版本中测试了您的代码。它工作正常。

      希望对您有所帮助。

      【讨论】:

      • @ShubhamBansal 好的。请接受我的回答以帮助其他人。 :)
      • 我们已经在使用最新版本 28.0.0。不过,这个问题正在发生!
      【解决方案4】:

      你应该为你的行为设置peekHeight

      【讨论】:

      • 可能会对答案进行一些详细说明
      • 例如:bottomSheetBehavior.setPeekHeight(size);
      【解决方案5】:

      我已按照这些步骤操作,这对BottomSheetDialog dialog 有帮助。

      Step1:要创建BottomSheetBehaviour,你需要默认存在的对话框的FrameLayout

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

      如果您使用的是 androidx,请使用

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

      第二步: 将底部工作表行为设置为展开状态并设置窥视高度

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

      第3步:不允许对话框在触摸外部时取消

      dialog.setCanceledOnTouchOutside(false);
      

      注意:您可以使用 DisplayMetrics 动态计算 screenHeght。

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

      【讨论】:

        【解决方案6】:

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

        AndroidX 使用

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

        而不是

        android.support.design.R.id.design_bottom_sheet
        

        【讨论】:

          【解决方案7】:

          不要使用片段使用BottomSheetDialog。 示例演示上传到 https://github.com/bita147/BottomSheetDialog

          为模型对话框设置

          bottomshetDialod.setCanceledOnTouchOutside(false);
          

          为我工作。

          【讨论】:

            【解决方案8】:

            同样的事情发生在我身上。背后的奇怪原因是Toolbar 如果您删除工具栏,则底页将全屏显示。

            我不知道背后的原因。但是删除工具栏后它工作正常。 你可以试试。

            【讨论】:

            • 我的猜测是它是屏幕高度的一个因素,因此当您删除工具栏时会有更多可用空间。就我而言,它仅在屏幕方向为水平时发生
            【解决方案9】:

            此代码对我有用。

            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
            }
            

            【讨论】:

            • 我忘了说我目前在kotlin开发。
            猜你喜欢
            • 1970-01-01
            • 2021-12-02
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-05-15
            • 1970-01-01
            • 1970-01-01
            • 2019-06-27
            相关资源
            最近更新 更多