20

I am trying to create a DialogFragment with a width of MATCH_PARENT so the dialog is nearly full screen (leaving the padding around the edges for the floating look). I have seen this solution Full Screen DialogFragment in Android but am trying to avoid the hack of setting the width to 1000dp. With my current layout, whether I use FILL_PARENT or MATCH_PARENT it seems to be setting the width and height to WRAP_CONTENT.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

I have used this solution for Dialog (not DialogFragment) and it works as expected:

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
4

10 回答 10

37

这对我来说是完美的。当 extends DialogFragment override onCreateView()... 实现所有逻辑

对话框让它全屏只是覆盖这个方法

 @Override
public Dialog onCreateDialog(final Bundle savedInstanceState) {

    // the content
    final RelativeLayout root = new RelativeLayout(getActivity());
    root.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

    // creating the fullscreen dialog
    final Dialog dialog = new Dialog(getActivity());
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(root);
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.YELLOW));
    dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

    return dialog;
}
于 2014-03-26T18:19:34.640 回答
21

尝试切换到 RelativeLayout 而不是 LinearLayout。它适用于我的情况

于 2012-08-09T16:56:19.653 回答
8
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(DialogFragment.STYLE_NO_TITLE,android.R.style.Theme_Holo_Light);
}
于 2015-06-22T12:20:07.460 回答
3

这个答案帮助我 如何设置DialogFragment的宽度和高度?

int width = activity.getResources().getDisplayMetrics().widthPixels;
int height = activity.getResources().getDisplayMetrics().heightPixels;
content.setLayoutParams(new LinearLayout.LayoutParams(width, height));
于 2013-11-01T13:56:58.197 回答
2

我发现的最佳解决方案是将宽度或 minWidth 设置为任意大的值,以确保它填满屏幕。我不喜欢这个解决方案,但我没有看到更好的解决方案。

于 2013-06-12T14:32:00.850 回答
2

在我的代码中,我想要一个全屏对话框,但仍然在屏幕顶部显示通知栏,所以我正在使用:

setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black_NoTitleBar);

这使它成为全屏,但我没有使用全屏样式,以便保留通知栏。

希望它可以帮助某人!

于 2016-04-16T22:43:19.437 回答
1

你也可以做类似的事情 -

@Override
  public void onCreate(@Nullable final Bundle savedInstanceState) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
      setStyle(STYLE_NO_TITLE, android.R.style.Theme_Material_Light_NoActionBar_Fullscreen);
    } else {
      setStyle(STYLE_NO_TITLE, android.R.style.Theme_DeviceDefault_Light_NoActionBar);
    }
    super.onCreate(savedInstanceState);

  }

这样,您还可以看到为版本 >= lollipop 着色的状态栏

于 2016-10-14T06:24:43.407 回答
0

我已经用解释回答了这个问题,这是最短和有效的方法。请检查在这个线程

希望这可以帮助 :)

于 2017-06-26T10:58:35.423 回答
0

在 DialogFragment 上进行以下工作。在onCreateView()添加以下

getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); 

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    //…
    getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    //…
    return view;
}
于 2016-07-13T13:50:18.453 回答
-1

要拥有全屏对话框,您需要使用

public class CustomDialogFragment extends DialogFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        // Inflate the layout to use as dialog or embedded fragment
        return inflater.inflate(R.layout.purchase_items, container, false);
    }


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        return dialog;
    }
}

并显示此对话框:

CustomDialogFragment newFragment = new CustomDialogFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
// To make it fullscreen, use the 'content' root view as the container
// for the fragment, which is always the root view for the activity
fragmentManager.beginTransaction().add(android.R.id.content, newFragment).commit();

注意:您不能使用以下代码显示对话框,否则,对话框将显示在屏幕中心,而不是全屏。

//don't show dialog like this
newFragment.show(fragmentManager, "dialog");
于 2016-09-08T07:53:20.010 回答