【问题标题】:BottomSheetDialogFragment opens half [duplicate]BottomSheetDialogFragment 打开一半[重复]
【发布时间】:2018-08-10 09:04:20
【问题描述】:

我的BottomSheetDialogFragment 打开时打开一半(意思是没有完全打开)。

fragment.show(supportFragmentManager, "my_frag")
  • 我尝试了NestedScrollViewbehavior_peekHeight,但没有成功。
  • 尝试不使用NestedScrollView。只有LinearLayout
  • 尝试在match_parentwrap_content 之间切换高度

我在BottomSheetDialogFragment 布局中有简单的RecyclerView

<android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            ...
            >
           <android.support.v7.widget.RecyclerView
           ...
           />

【问题讨论】:

    标签: android android-layout bottom-sheet


    【解决方案1】:

    BottomSheetFragment 是指 BottomSheetDialogFragment。要打开已使用的工作表,您需要在 onCreateDialog() 中进行一些更改。

     @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        BottomSheetDialog bottomSheetDialog=(BottomSheetDialog)super.onCreateDialog(savedInstanceState);
        bottomSheetDialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                BottomSheetDialog dialog = (BottomSheetDialog) dialog;
                FrameLayout bottomSheet =  dialog .findViewById(android.support.design.R.id.design_bottom_sheet);
                BottomSheetBehavior.from(bottomSheet).setState(BottomSheetBehavior.STATE_EXPANDED);
                BottomSheetBehavior.from(bottomSheet).setSkipCollapsed(true);
                BottomSheetBehavior.from(bottomSheet).setHideable(true);
            }
        });
        return bottomSheetDialog;
    }
    

    保持布局match_parent 不需要使用NestedScrollView。它对我有用。如果您仍然遇到问题,请告诉我。

    万一有人在使用新材料库。这是
    implementation 'com.google.android.material:material:1.0.0'。 然后您需要更改 Parent FrameLayout 的 id。所以会的。

     @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        BottomSheetDialog bottomSheetDialog=(BottomSheetDialog)super.onCreateDialog(savedInstanceState);
        bottomSheetDialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dia) {
                BottomSheetDialog dialog = (BottomSheetDialog) dia;
                FrameLayout bottomSheet =  dialog .findViewById(com.google.android.material.R.id.design_bottom_sheet);
                BottomSheetBehavior.from(bottomSheet).setState(BottomSheetBehavior.STATE_EXPANDED);
                BottomSheetBehavior.from(bottomSheet).setSkipCollapsed(true);
                BottomSheetBehavior.from(bottomSheet).setHideable(true);
            }
        });
        return bottomSheetDialog;
    }
    

    在这种情况下,请确保您从 import com.google.android.material 导入的所有内容。

    【讨论】:

    • 所以一个小东西需要用6行代码来修复。这对 Android 很不利。
    • 在某种程度上它很好(只是为了满足所有要求)。您会看到标志 setSkipCollapsedsetHideable 在您不想将工作表锚定在两者之间时非常有用。而且肯定还有更多。
    • 如果您在底部表格中有一个EditText(如聊天视图)并且您对SoftInputMode 感到困惑,请关注This answer。我已经浪费了 2 天的时间来获得它。认为它可能值得分享。除了这个,没有其他解决方案对我有用。
    • @ADM 你拯救了我的一天,谢谢 :)
    • 当之无愧的男人!!
    【解决方案2】:

    您正在访问您的父视图,因此请使用以下代码将其展开为全屏。

    View parent = (View) inflatedView.getParent();
    parent.setFitsSystemWindows(true);
    BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(parent);
    inflatedView.measure(0, 0);
    DisplayMetrics displaymetrics = new DisplayMetrics();        getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    int screenHeight = displaymetrics.heightPixels;
    bottomSheetBehavior.setPeekHeight(screenHeight);
    
    if (params.getBehavior() instanceof BottomSheetBehavior) {
        ((BottomSheetBehavior)params.getBehavior()).setBottomSheetCallback(mBottomSheetBehaviorCallback);
    }
    
    params.height = screenHeight;
    parent.setLayoutParams(params);
    

    希望对你有所帮助。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-20
    • 2020-11-23
    • 2020-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多