LoginSignup
12
11

More than 5 years have passed since last update.

BottomSheetDialogFragmentのPeekHeightを変更する方法

Posted at

概要

BottomSheetBehaviorを使用してBottomSheetを実装する場合には setPeekHeight() を使うことでPeekHeightが変更できますが、
BottomSheetDialogFragmentを使用してModalのBottomSheetを実装する場合には少しやり方が異なるため、今回はその方法を紹介します。

方法

以下のようなStyleを作成し、BottomSheetDialogFragmentを継承したクラス内で設定すれば、PeekHeightを変更することができます。

style.xml
<style name="BottomSheetDialog" parent="Theme.Design.Light.BottomSheetDialog">
    <item name="bottomSheetStyle">@style/bottomSheetStyleWrapper</item>
</style>

<style name="bottomSheetStyleWrapper" parent="Widget.Design.BottomSheet.Modal">
    <!-- この値がPeekHeightになります。デフォルトだと256dpです。 -->
    <item name="behavior_peekHeight">312dp</item>
</style>
CustomBottomSheetDialogFragment.java
public class CustomBottomSheetDialogFragment extends BottomSheetDialogFragment {

    public CustomBottomSheetDialogFragment() {
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // style.xmlに作ったStyleをセットする
        setStyle(BottomSheetDialogFragment.STYLE_NORMAL, R.style.BottomSheetDialog);
        return super.onCreateDialog(savedInstanceState);
    }

    @Override
    public void setupDialog(Dialog dialog, int style) {
        // 省略
    }
}

参考資料

12
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
11