Problem inflating custom view for AlertDialog in DialogFragment

后端 未结 9 2312
忘了有多久
忘了有多久 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:51

    What you want to do instead is create your custom view in the onCreateView method like you normally would. If you want to do something like change the title of the dialog, you do that in onCreateView.

    Here's an example to illustrate what I mean:

            @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            getDialog().setTitle("hai");
            View v = inflater.inflate(R.layout.fragment_dialog, container, false);
            return v;
        }
    

    Then you just call:

    DialogFragment df = new MyDialogFragment();
    df.show(..);
    

    And voila, a dialog with your own custom view.

提交回复
热议问题