How to make DialogFragment width to Fill_Parent

前端 未结 18 2519
栀梦
栀梦 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:13

    In my case I also used the following approach:

        @Override
        public void onStart() {
            super.onStart();
            getDialog().getWindow().setLayout(LayoutParams.MATCH_PARENT, (int) getResources().getDimension(R.dimen.dialog_height));
        }
    }
    

    But there were still small gaps between left and right edges of the dialog and the screen edges on some Lollipop+ devices (e.g. Nexus 9).

    It was not obvious but finally I figured out that to make it full width across all the devices and platforms window background should be specified inside styles.xml like the following:

    
    

    And of course this style needs to be used when we create the dialog like the following:

        public static DialogFragment createNoTitleDlg() {
            DialogFragment frag = new Some_Dialog_Frag();
            frag.setStyle(DialogFragment.STYLE_NO_TITLE, R.style.Dialog_NoTitle);
            return frag;
    }
    

提交回复
热议问题