Custom Dialog in full screen?

前端 未结 11 2039
终归单人心
终归单人心 2020-12-08 13:35

Is there any way to make my Dialog view full screen, i.e dialog occupy the entire screen (like an Activity). I tried using the LayoutParams and styles like

11条回答
  •  南方客
    南方客 (楼主)
    2020-12-08 14:05

    Here are steps how to create a full screen dialog with custom layout which occupies the entire screen without any padding from each site.

    Step 1: Define your custom dialog layout named layout_fullscreen_dialog.xml

    
    
    
        
    
    
    

    Step 2: Define a new style in styles.xml named FullScreenDialog

    
    

    Step 3: Write a method which creates and shows a dialog

    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            createAndShowDialog(this);
        }
    
        // This method used to create and show a full screen dialog with custom layout.
        public void createAndShowDialog(Context context) {
            Dialog dialog = new Dialog(context, R.style.FullScreenDialog);
            dialog.setContentView(R.layout.layout_fullscreen_dialog);
    
            WindowManager.LayoutParams layoutParams = dialog.getWindow().getAttributes();
            dialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
            dialog.getWindow().setAttributes(layoutParams);
            dialog.show();
        }
    }
    

提交回复
热议问题