平板电脑横向中的 BottomSheetDialogFragment

BottomSheetDialogFragment In Tablet Landscape

是否可以更改 BottomSheetDialogFragment(material 设计)的宽度?在平板横向模式下,宽度为 match_parent。谢谢

Bottom sheets look great on phones. But when putting them on tablets, they feel stretched out, especially in landscape mode. It’s due to the high ratio between peek height vs full tablet width. Details matter, and we want our tablet users as happy as phone users. Let’s customize bottom sheet width for tablets.

values-w820dp/dimens.xml

<resources>
  <dimen name="bottom_sheet_width">600dp</dimen>
</resources>

片段文件

public class PopupSettingsFragment extends AppCompatDialogFragment {
  ...

  @Override
  public Dialog onCreateDialog(Bundle savedInstanceState) {
    return new CustomWidthBottomSheetDialog(getActivity(), getTheme());
  }

  static class CustomWidthBottomSheetDialog extends BottomSheetDialog {
    public CustomWidthBottomSheetDialog(@NonNull Context context, @StyleRes int theme) {
      super(context, theme);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      int width = getContext().getResources().getDimensionPixelSize(R.dimen.bottom_sheet_width);
      getWindow().setLayout(width > 0 ? width : ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.MATCH_PARENT);
    }
  }
}

Source