android - BottomSheetDialogFragment 在横向模式下不显示全高

标签 android

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

Portrait Mode

Landscape Mode

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

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

CustomBottomSheetDialog layout

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

在横向模式下,我必须拖动 BottomSheetDialogFragment 才能看到全部内容。

最佳答案

这个问题的解决方案是。

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

关于android - BottomSheetDialogFragment 在横向模式下不显示全高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45614271/

相关文章:

android - 如何在 Android 中以离线模式显示 HTML 图像标签

android - 如何拥有带有数字字符的 TextView

android - 如何着手制作 Android MP3 播放器?

android - 解析日期在不同的 Android 版本上有不同的值

android - emptyView 未被 listView 替换

android - Recyclerview 从上边距削减

android - 无法在浏览器中打开本地文件

android - 如何从android中的线程获取结果?

Android Studio 状态栏着色不起作用

android - 数据未显示在 ListView 中