Customization Bottom Sheet Dialog's View

前端 未结 2 1951
迷失自我
迷失自我 2021-02-09 23:54

I just want to get the bottom sheet dialog like below_: margin from the system window. How can I get like this?

2条回答
  •  误落风尘
    2021-02-10 00:06

    You can create Bottom Sheet Dialog Fragment in following way:

    First create the xml file as below named as

    fragment_bottomsheet

    
    
    
        
    
            
    
        
    
    
    

    Now create a Bottom Sheet Fragment named as

    BottomSheetFragment

    import android.app.Dialog;
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.support.design.widget.BottomSheetDialogFragment;
    import android.view.View;
    
    public class BottomSheetFragment extends BottomSheetDialogFragment {
    
        public static BottomSheetFragment newInstance() {
            BottomSheetFragment fragment = new BottomSheetFragment();
            return fragment;
        }
    
        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }
    
        @Override
        public void setupDialog(Dialog dialog, int style) {
            View contentView = View.inflate(getContext(), R.layout.fragment_bottomsheet, null);
            dialog.setContentView(contentView);
            ((View) contentView.getParent()).setBackgroundColor(getResources().getColor(android.R.color.transparent));
        }
    
    }
    

    To call that bottom sheet fragment you can write as below:

    BottomSheetFragment bottomSheetDialog = BottomSheetFragment.newInstance();
    bottomSheetDialog.show(getSupportFragmentManager(), "Bottom Sheet Dialog Fragment");
    

    I have only took a single textview for now and attaching the screenshot because your main concern is to get margin in bottomsheet. Also in this way you can customize bottom sheet as you want. Thanks!

提交回复
热议问题