如何显示一个全屏的DialogFragment?

12
我正在尝试创建一个自定义的 DialogFragment,它可以延伸到整个屏幕的宽度(或者说是父片段)。虽然我可以使 DialogFragment 的边框透明,但仍然存在左右两侧无法去除的填充。
这是我的代码片段:
public static class LoaderDialog extends DialogFragment {

    static LoaderDialog newInstance() {
        LoaderDialog f = new LoaderDialog();

        return f;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.loader_f, container, false);

        WindowManager.LayoutParams p = getDialog().getWindow().getAttributes();
        p.y = getSupportActionBar().getHeight();

        getDialog().getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        getDialog().getWindow().setGravity(Gravity.TOP);
        getDialog().getWindow().setAttributes(p);

        return view;
    }
}

这是一张图片,它的样子如下: screenshot

你可以看到,DialogFragment(红色部分)两侧有一些边距。我想让它们消失。有任何想法吗?(如果可能的话,请用 Java 实现。)


可能是Android对话框宽度的重复问题。 - user
谢谢提供链接。但是我之前尝试过 p.width = LayoutParams.FILL_PARENT;,但没有成功。所以 UMAR 的答案似乎对我没有帮助。 - janoliver
5个回答

14

您可以使用:

WindowManager.LayoutParams wmlp = getDialog().getWindow().getAttributes();
wmlp.gravity = Gravity.FILL_HORIZONTAL; 

完整示例:

public class TextEditor extends DialogFragment {

    public TextEditor () {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_text_editor, container);

        WindowManager.LayoutParams wmlp = getDialog().getWindow().getAttributes();
        wmlp.gravity = Gravity.FILL_HORIZONTAL;

        return view;
    }
}

这个方案可行,但根布局不应该是Coordinator Layout。 - Vivek
非常感谢。像宝石一样工作。 - Pranav P

0

使用此样式来创建 DialogFragment

<item name="android:windowNoTitle">true</item>
<item name="android:padding">0dp</item>

或者在 DialogFragment 的 onCreateView 方法中使用此代码

    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 是 DialogFragment 的高度,您可以根据需要进行更改。


0

试试这个:

p.horizontalMargin = 0;

0
在你的 style.xml 文件中创建一个样式元素。将下面的代码复制到你的 style.xml 文件中。
<style name="CustomDialog" parent="@android:style/Theme.Holo.Light" >
<item name="android:windowBackground">@null</item>
<item name="android:windowIsFloating">true</item>

Then in the createDialog method of your DialogFragment class,

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

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


-1

尝试使用LayoutParams.MATCH_PARENT代替。Fill_parent已经过时了。此外,如果您为视图设置了填充,则正常情况下不会填充其父视图。


MATCH_PARENT 会产生相同的结果。 - Eduard
是的,但如果您要为Android 2.2+开发应用程序,则应选择MATCH_PARENT而不是FILL_PARENT。重命名发生在API级别8中。 - Substitut
2
这完全不重要,因为它们在代码中表示相同的值(精确地说是-1)。 - scana

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接