How to make DialogFragment width to Fill_Parent

前端 未结 18 2380
栀梦
栀梦 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 08:57

    Have you tried using the answer of Elvis from How to make an alert dialog fill 90% of screen size?

    It is the following:

    dialog.getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    

    Update:

    Above code should be added inside onStart() method of the DialogFragment.

    0 讨论(0)
  • 2020-12-04 08:58
    public class FullWidthDialogFragment extends AppCompatDialogFragment {
        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setStyle(DialogFragment.STYLE_NORMAL, R.style.Theme_AppCompat_Light_Dialog_Alert);
        }
    }
    

    and if you want much more flexibility can extend AppCompat_Dialog_Alert and custom attributes

    0 讨论(0)
  • 2020-12-04 09:02

    For me, it worked when I replaced the LinearLayout parent of the layout inflated in onCreateView by RelativeLayout. No other code change required.

    0 讨论(0)
  • 2020-12-04 09:04

    Based on other solutions, I have created my own.

    <style name="AppTheme.Dialog.Custom" parent="Theme.AppCompat.Light.Dialog.Alert">
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowMinWidthMajor">100%</item>
        <item name="android:windowMinWidthMinor">100%</item>
    </style>
    
    abstract class BaseDialogFragment : DialogFragment() {
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setStyle(DialogFragment.STYLE_NO_TITLE, R.style.AppTheme_Dialog_Custom)
        }
    }
    
    0 讨论(0)
  • 2020-12-04 09:05

    This is worked for me

    Create your custom style :

       <style name="DialogStyle" parent="Base.Theme.AppCompat.Dialog">
        <item name="android:windowMinWidthMajor">97%</item>
        <item name="android:windowMinWidthMinor">97%</item>
       </style>
    

    You can also try use the right parent to match your other dialogs. for example parent="ThemeOverlay.AppCompat.Dialog" and so on.

    Use this style in your dialog

    public class MessageDialog extends DialogFragment {
    
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.DialogStyle);
    }
    
    // your code 
    }
    
    0 讨论(0)
  • 2020-12-04 09:06
        <!-- A blank view to force the layout to be full-width -->
        <View
            android:layout_width="wrap_content"
            android:layout_height="1dp"
            android:layout_gravity="fill_horizontal" />
    

    in the top of my dialog layout did the trick.

    0 讨论(0)
提交回复
热议问题