19
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Android Support Library 23.2のBottomSheetDialogの高さについて

Last updated at Posted at 2016-02-29

最初に

ModalなBottomSheetsを実装するのに、サード・パーティのライブラリを使っていた。
先日のDroidKaigi2016で、Support LibraryにBottomSheetsが実装されると聞き、待っていたらすぐリリリースされた!
なので、さっそくライブラリを置き換えてみた。

問題発生

通常のDialogFragmentをBottomSheetDialogFragmentに変えて実装してみた。

        @Override
        public void setupDialog(Dialog dialog, int style) {
            super.setupDialog(dialog, style);
            mContentView = View.inflate(getContext(), R.layout.dialog_content, null);
            dialog.setContentView(mContentView);
        }

そうすると、このようにDialogの中身が全部表示されない。。。
Screenshot_20160301-001010.png

問題解決

色々と試してみた結果、以下のように書くと中身が全部表示された。(∩´∀`)∩ワーイ

        @Override
        public void setupDialog(Dialog dialog, int style) {
            super.setupDialog(dialog, style);
            mContentView = View.inflate(getContext(), R.layout.dialog_content, null);
            dialog.setContentView(mContentView);

            mListener = new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    // BottomSheetの高さなどを設定
                    View parent = (View) mContentView.getParent();
                    BottomSheetBehavior behavior = BottomSheetBehavior.from(parent);
                    behavior.setPeekHeight(mContentView.getMeasuredHeight());
                    CoordinatorLayout.LayoutParams layoutParams =
                        (CoordinatorLayout.LayoutParams) ((View) mContentView.getParent()).getLayoutParams();
                    layoutParams.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;

                    // OnGlobalLayoutListenerを外す
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                        removeGlobalLayoutListener();
                    } else {
                        removeGlobalLayoutListenerCompat();
                    }
                }
            };
            mContentView.getViewTreeObserver().addOnGlobalLayoutListener(mListener);
        }

Screenshot_20160301-001330.png


サンプルのコードは、GitHubで公開しています。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?