How to make DialogFragment width to Fill_Parent

2019-01-12 22:30发布

Hello 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 fill_parent to it ?

public class AddNoteDialogFragment extends DialogFragment {

    public AddNoteDialogFragment() {
        // Empty constructor required for DialogFragment
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        getDialog().setTitle(getString(R.string.app_name));
        View view = inflater.inflate(R.layout.fragment_add_note_dialog,
                container);

        return view;
    }


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);

        // request a window without the title
        dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);

        return dialog;
    }
}

fragment_add_note_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

    <EditText
        android:id="@+id/addNoteEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="top"
        android:hint="@string/clock_enter_add_note"
        android:imeOptions="actionDone"
        android:inputType="textCapSentences|textMultiLine"
        android:lines="5" />

    <Button
        android:id="@+id/submit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="@drawable/login_button"
        android:text="@string/submit_button"
        android:textColor="@android:color/white" />

</LinearLayout>

enter image description here

Thanks in advance.

13条回答
劳资没心,怎么记你
2楼-- · 2019-01-12 22:42

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));
查看更多
地球回转人心会变
3楼-- · 2019-01-12 22:43
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

查看更多
何必那么认真
4楼-- · 2019-01-12 22:46

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>

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 
}
查看更多
Rolldiameter
5楼-- · 2019-01-12 22:49

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

查看更多
Luminary・发光体
6楼-- · 2019-01-12 22:50

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;
}
查看更多
虎瘦雄心在
7楼-- · 2019-01-12 22:52

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

查看更多
登录 后发表回答