Prevent BottomSheetDialogFragment covering navigation bar

前端 未结 10 493
轮回少年
轮回少年 2020-12-12 23:49

I\'m using really naive code to show a bottom sheet dialog fragment:

class LogoutBottomSheetFragment : BottomSheetDialogFragment() {

    override fun onCrea         


        
10条回答
  •  無奈伤痛
    2020-12-13 00:25

    I had the same problem. After looking into sources I found a workaround (a little bit hacky, but I found no alternatives).

    public class YourDialog extends BottomSheetDialogFragment {
    
        //your code
    
        @NonNull
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            return new FitSystemWindowsBottomSheetDialog(getContext());
        }
    }
    
    public class FitSystemWindowsBottomSheetDialog extends BottomSheetDialog {
    
        public FitSystemWindowsBottomSheetDialog(Context context) {
            super(context);
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            if (getWindow() != null && Build.VERSION.SDK_INT >= 21) {
                findViewById(android.support.design.R.id.coordinator).setFitsSystemWindows(false);
                findViewById(android.support.design.R.id.container).setFitsSystemWindows(false);
                getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS |
                        WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            }
        }
    }
    

    And, finally, don't forget to add android:fitsSystemWindows="true" at the root of your dialog layout.

    Hope it helps.

提交回复
热议问题