Full screen DialogFragment

試著忘記壹切 提交于 2019-12-03 09:28:52

问题


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);

回答1:


this is perfect for me. when extends DialogFragment override onCreateView()... impelments all logic

dialog make it Full Screen just override this method

 @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;
}



回答2:


try to switch to RelativeLayout instead of LinearLayout. It worked in my case




回答3:


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(DialogFragment.STYLE_NO_TITLE,android.R.style.Theme_Holo_Light);
}



回答4:


this answer help me How to set DialogFragment's width and height?

int width = activity.getResources().getDisplayMetrics().widthPixels;
int height = activity.getResources().getDisplayMetrics().heightPixels;
content.setLayoutParams(new LinearLayout.LayoutParams(width, height));



回答5:


The best solution I found was to set the width or minWidth to some arbitrarily large value to ensure it fills the screen. I don't like this solution, but I haven't seen anything better.




回答6:


In my code, I was wanting a fullscreen dialog, but still show the notification bar at the top of the screen, so I'm using:

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

This makes it fullscreen, but I'm not using a fullscreen style so that I keep the notification bar.

Hope it helps someone!




回答7:


You can also do something like-

@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);

  }

By this you will also be able to see the status bar colored for versions >= lollipop




回答8:


I have answered this question with explanation this is the shortest and efficient way. Please check In this thread

Hope this helps :)




回答9:


On DialogFragment following works. In onCreateView() add following

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;
}



回答10:


To have a full screen dialog, you need use

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;
    }
}

And to show this 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();

ATTENTION: You can't use following code to show the dialog, if not, the dialog will be shown in center of screen, not full screen.

//don't show dialog like this
newFragment.show(fragmentManager, "dialog");


来源:https://stackoverflow.com/questions/11887340/full-screen-dialogfragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!