Problem inflating custom view for AlertDialog in DialogFragment

后端 未结 9 2425
忘了有多久
忘了有多久 2020-11-27 04:43

I\'m trying to create a DialogFragment using a custom view in an AlertDialog. This view must be inflated from xml. In my DialogFragment

9条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-27 04:52

    I'm surprised by these answers as none of them solve the problem.

    A DialogFragment allows you to reuse the same UI for both a dialog and integrated in your app elsewhere as a fragment. Quite a useful feature. As per google's documentation, you can achieve this by overriding onCreateDialog and onCreateView. http://developer.android.com/reference/android/app/DialogFragment.html

    There are three scenarios here:

    1. Override onCreateDialog only - Works as a dialog but cannot be integrated elsewhere.
    2. Override onCreateView only - Does not work as a dialog but can be integrated elsewhere.
    3. Override both - Works as a dialog and can be integrated elsewhere.

    Solution: The AlertDialog class is calling another class which calls requestFeature. To fix this.. Don't use the AlertDialog, instead use a plain Dialog or whatever super.onCreateDialog returns. This the solution that I have found works best.

    Caveat: Other dialogs such as DatePickerDialog, ProgressDialog, TimePickerDialog all inherit from AlertDialog and will likely cause the same error.

    Bottom Line: DialogFragment is good if you need to create very customized interface that needs to be used in several places. It doesn't appear to work to reuse existing android dialogs.

提交回复
热议问题