BottomSheetDialogFragment在横向模式下不显示完整高度

Gau*_*bde 10 android

我在我的活动中使用BottomSheetDialogFragment,对话框在纵向模式下显示全高,但在切换到横向模式时不显示.

肖像模式

景观模式

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        CustomBottomSheetDialog customBottomSheetDialog = new CustomBottomSheetDialog();
        customBottomSheetDialog.show(getSupportFragmentManager(),customBottomSheetDialog.getTag());
    }
}
Run Code Online (Sandbox Code Playgroud)

CustomBottomSheetDialog

public class CustomBottomSheetDialog extends BottomSheetDialogFragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return View.inflate(getContext(), R.layout.view_config, null);
    }
}
Run Code Online (Sandbox Code Playgroud)

CustomBottomSheetDialog布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:background="#fdf107"
              android:layout_height="wrap_content">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="196dp"
        android:gravity="center"
        android:textColor="@color/colorAccent"
        android:text="BottomSheetDialogFragment"/>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

在横向模式下,我必须拖动BottomSheetDialogFragment才能看到整个内容.

ave*_*raj 24

这个问题的解决方案是.

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            if (Build.VERSION.SDK_INT < 16) {
                view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            } else {
                view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
            BottomSheetDialog dialog = (BottomSheetDialog) getDialog();
            FrameLayout bottomSheet = (FrameLayout)
            dialog.findViewById(android.support.design.R.id.design_bottom_sheet);
            BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);
            behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
            behavior.setPeekHeight(0); // Remove this line to hide a dark background if you manually hide the dialog.
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

  • 如果您使用的是AndroidX库,则应使用com.google.android.material.R.id.design_bottom_sheet (5认同)
  • 一旦事件被触发,不要忘记取消注册侦听器. (4认同)
  • 谢谢!我对此有疑问:对话框隐藏时,停电不会消失。注释掉这一行对我有帮助:`behavior.setPeekHeight(0);` (2认同)

Mak*_*osH 13

这对我有用,是最干净的方法:

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
    val dialog = BottomSheetDialog(requireContext(), theme)
    dialog.behavior.state = BottomSheetBehavior.STATE_EXPANDED
    return dialog
}
Run Code Online (Sandbox Code Playgroud)

  • 在最近的api中dialog.behavior没有暴露,它是私有的 (2认同)
  • 您还可以将第二行替换为“valdialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog”。 (2认同)
  • 如果在尝试关闭或隐藏对话框时出现折叠状态,请添加以下内容:“dialog.behavior.skipCollapsed = true” (2认同)

cle*_*vor 6

ViewTreeObserver解决方案对我不起作用,但我在这里找到了一个更好的解决方案并将其转换为 Kotlin。这个没有昂贵的计算浪费,ViewTreeObserver并且很好地将功能捆绑到类中。

class ExpandedBottomSheetDialog(context: Context) : BottomSheetDialog(context) {

    override fun show() {
        super.show()
        // androidx should use: com.google.android.material.R.id.design_bottom_sheet
        val view = findViewById<View>(R.id.design_bottom_sheet)
        view!!.post {
            val behavior = BottomSheetBehavior.from(view)
            behavior.setState(BottomSheetBehavior.STATE_EXPANDED)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 此解决方案不适用于BottomSheetDialog`Fragment`,因为它使用未附加到任何`view` 的`findViewById`(在此阶段为`null`)。此外,如果您覆盖 `show(manager: FragmentManager?, tag: String?)`(请参阅 /sf/ask/1101039691/ -action-after-onsaveinstancestate) 并使用 `super.show(manager, tag)`,那么你可能会捕获异常。 (5认同)

Coo*_*ind 6

多亏@avez raj和我在中撰写的《与外界接触防止BottomSheetDialogFragment被解雇》onCreateDialog()

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
    val dialog = super.onCreateDialog(savedInstanceState)

    dialog.setOnShowListener {
        // For AndroidX use: com.google.android.material.R.id.design_bottom_sheet
        val bottomSheet = dialog.findViewById<View>(
            android.support.design.R.id.design_bottom_sheet) as? FrameLayout
        val behavior = BottomSheetBehavior.from(bottomSheet)
        behavior.state = BottomSheetBehavior.STATE_EXPANDED
    }

    return dialog
}
Run Code Online (Sandbox Code Playgroud)

  • 为什么会发生呢?什么情况下会变成这样?有官方的处理方法吗?也许我们应该在它没有扩展时以不同的方式处理它?或者这是一个错误?我也注意到 Play 商店上有一个与之相关的奇怪错误:https://issuetracker.google.com/issues/133809189 (2认同)
  • @androiddeveloper,我想,谷歌公司的他们每天都会制作奇怪的伙伴组件。程序员必须为每个错误创建解决方法。我的前同事也注意到全屏键盘下 BSDF 的问题,甚至想用 Fragment 而不是 BSDF 重写。 (2认同)

小智 5

您可以简单地执行以下操作:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    dialog?.let {
        val sheet = it as BottomSheetDialog
        sheet.behavior.state = BottomSheetBehavior.STATE_EXPANDED
    }

    // rest of your stuff
}
Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

6951 次

最近记录:

6 年,6 月 前