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:54
    <!-- 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.

查看更多
唯我独甜
3楼-- · 2019-01-12 22:56

Another solution that work for me without side effects was:

In your class that extends DialogFragment:

@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setStyle(DialogFragment.STYLE_NO_TITLE, R.style.DialogStyle);
}

In your styles:

<style name="DialogStyle" parent="ThemeOverlay.AppCompat.Dialog.Alert"></style>
查看更多
Animai°情兽
4楼-- · 2019-01-12 22: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);
}
查看更多
5楼-- · 2019-01-12 22:56

use this in onCreateView method of DialogFragment

    Display display = getActivity().getWindowManager().getDefaultDisplay();
    int width = display.getWidth();
    int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, **220**, getResources().getDisplayMetrics());
    getDialog().getWindow().setLayout(width,px);

220 is dialog fragment height, change it as u wish

查看更多
家丑人穷心不美
6楼-- · 2019-01-12 22:58

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.FILL_PARENT, LayoutParams.FILL_PARENT);

Update:

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

LayoutParams.FILL_PARENT is deprecated use LayoutParams.MATCH_PARENT instead.

查看更多
我想做一个坏孩纸
7楼-- · 2019-01-12 22:58

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:

<style name="Dialog.NoTitle" parent="Theme.AppCompat.Dialog">
    <item name="android:windowNoTitle">true</item>
    <item name="android:padding">0dp</item>
    <item name="android:windowBackground">@color/window_bg</item>
</style>

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;
}
查看更多
登录 后发表回答