How to create a DialogFragment without title?

后端 未结 6 1797
时光说笑
时光说笑 2020-11-29 15:50

I\'m creating a DialogFragment to show some help messages regarding my app. Everything works fine besides one thing: There is a black stripe at the top of the window that sh

相关标签:
6条回答
  • 2020-11-29 15:57

    Just add this line of code in your HelpDialog.onCreateView(...)

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

    This way you're explicitly asking to get a window without title :)


    EDIT

    As @DataGraham and @Blundell pointed out on the comments below, it's safer to add the request for a title-less window in the onCreateDialog() method instead of onCreateView(). This way you can prevent ennoying NPE when you're not using your fragment as a Dialog:

    @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;
    }
    
    0 讨论(0)
  • 2020-11-29 16:01

    Try easy way

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(STYLE_NO_TITLE, 0);
    }
    
    0 讨论(0)
  • 2020-11-29 16:04

    Dialog fragment has setStyle method, which should be called before view creation Java Doc. Also style of the dialog can be set with the same method

    public static MyDialogFragment newInstance() {
            MyDialogFragment mDialogFragment = new MyDialogFragment();
            //Set Arguments here if needed for dialog auto recreation on screen rotation
            mDialogFragment.setStyle(DialogFragment.STYLE_NO_TITLE, 0);
            return mDialogFragment;
    }
    
    0 讨论(0)
  • 2020-11-29 16:07
    FragmentManager manager = getSupportFragmentManager();
    SettingsDialog sd = new SettingsDialog();
    sd.setStyle(DialogFragment.STYLE_NO_TITLE, 0);
    sd.show(manager, "settings_dialog");
    
    0 讨论(0)
  • 2020-11-29 16:18

    Set the style to Theme_Holo_Dialog_NoActionBar:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(STYLE_NORMAL, android.R.style.Theme_Holo_Dialog_NoActionBar);
    }
    
    0 讨论(0)
  • 2020-11-29 16:19
    public class LoginDialog extends DialogFragment {   
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.login_dialog, null);
            getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
            return view;
        }   
    }
    
    0 讨论(0)
提交回复
热议问题