How to make DialogFragment width to Fill_Parent

前端 未结 18 2517
栀梦
栀梦 2020-12-04 08:21

I am working on an android application where I am using DialogFragment to display the dialog but its width is very small. How I can make this width to fil

18条回答
  •  悲哀的现实
    2020-12-04 09:06

    option 1. add following code in oncreate in activity

     getDialog().getWindow()
             .setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
    

    or you can Create a custom style for Dialog

    
    
    then use that style in dialog fragment
    
    @Override public void onCreate(@Nullable Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setStyle(DialogFragment.STYLE_NORMAL, R.style.CustomDialog);
    }
    
    @Override public void onStart() {
      super.onStart();
      getDialog().getWindow()
        .setLayout(WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.MATCH_PARENT);
    }
    
    SampleDialogFragment sampleDialogFragment = new SampleDialogFragment();
    SampleDialogFragment.show(getActivity().getSupportFragmentManager(), "sometag");
    

    OR you try the following code in style will help you

    @null
    

    Solution 2: dialog.window.setLayout

    class TestDialog : DialogFragment() {
    
        override fun onStart() {
            super.onStart()
    
            dialog?.window?.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)
        }
    
    }
    

提交回复
热议问题