How to make DialogFragment width to Fill_Parent

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

    This is the solution I figured out to handle this issue:

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);    
        dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    
        return dialog;
    }
    
    @Override
    public void onStart() {
        super.onStart();
    
        Dialog dialog = getDialog();
        if (dialog != null) {
           dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
           dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        }
    }
    

    Edit:

    You can use the code below in onCrateView method of Fragment before return the inflated view.

    getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
    
    0 讨论(0)
  • 2020-12-04 08:51

    Create a custom style in your style.xml file. Just copy paste this code into your style.xml file

    <style name="CustomDialog" parent="@android:style/Theme.Holo.Light" >
        <item name="android:windowBackground">@null</item>
        <item name="android:windowIsFloating">true</item>
    </style>
    

    Then in the createDialog method of your DialogFragment, create the dialog object by the code

     dialog = new Dialog(getActivity(), R.style.CustomDialog);
    

    This is working for me and hope this will help you too

    0 讨论(0)
  • 2020-12-04 08:52

    User AlertDialog.Builder inside your DialogFragment. Add it in onCreateDialog method like this. And in onCreateView do nothing.

    public Dialog onCreateDialog(Bundle savedInstanceState)
    {
    
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        View view = LayoutInflater.from(getContext()).inflate(R.layout.gf_confirm_order_timeout_dialog, null);
        final Bundle args = getArguments();
        String message = args.getString(GfConstant.EXTRA_DATA);
        TextView txtMessage = (TextView) view.findViewById(R.id.txt_message);
        txtMessage.setText(message);
    
        view.findViewById(R.id.btn_confirm).setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if (mListener != null)
                {
                    mListener.onDialogConfirmOK();
                }
            }
        });
        builder.setView(view);
        Dialog dialog = builder.create();
        dialog.setCanceledOnTouchOutside(false);
        dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        return dialog;
    }
    
    0 讨论(0)
  • 2020-12-04 08:55

    in your layout root, set android:minWidth to a very large value e.g

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:minWidth="9999999dp">
    
       ...
    
    </LinearLayout>
    
    0 讨论(0)
  • 2020-12-04 08:56

    I want clarify this. Both the ways are right, But with different DialogFragment.

    Using android.app.DialogFragment

    @Override
    public void onStart()
    {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null)
        {
            int width = ViewGroup.LayoutParams.MATCH_PARENT;
            int height = ViewGroup.LayoutParams.MATCH_PARENT;
            dialog.getWindow().setLayout(width, height);
        }
    }
    

    Using android.support.v4.app.DialogFragment

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
    }
    
    0 讨论(0)
  • 2020-12-04 08:56

    This works for me perfectly.

    android:minWidth="300dp"
    

    Through this you can give the width to dialog Fragment.

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