Little problem, Record ~
Android4.0 later began to recommend the use of Dialogfragment instead of dialog. Two examples are given in the official Android documentation:
- One
Basic Dialog
An example of how to customize the window Content-Override onCreateView
method.
- One
Alert Dialog
An example of how to customize the pop-up button-override onCreateDialog
method.
Okay, so here's the problem.
In practice, it is often necessary to customize both the window content and the custom button.
At this time if we retrace, Dialogfragment Oncreateview and Oncreatedialog methods are rewritten, will find--bong! Exceptions such as "androidruntimeexception:requestfeature () must be called before adding content" appear.
The reason for this anomaly can be seen: StackOverflow's answer to this question is freerider and the comments below.
Excerpt: "You can override both (in fact the Dialogfragment says so), the problem comes when you try to inflate the view afte R has already creating the dialog view. You can still does other things in Oncreateview, like use the savedinstancestate, without causing the exception.
Then there is the solution:
Since two methods cannot be overridden at the same time, choose one to rewrite:
Overriding the Oncreatedialog method, you can customize the button by referencing the official example, and then modify it so that it can be customized view-- create()
before Alertdialog.builder creates dialog. Use the Alertdialog.builder setView(view)
method, where view is a custom view.
To feel the difference ~
Here's an example for Google:
@Overridepublic Dialog Oncreatedialog (Bundle savedinstancestate) { int title = Getarguments (). GetInt ("title"); return new Alertdialog.builder (Getactivity ()) . SetIcon (R.drawable.alert_dialog_icon) . Settitle (title) . Setpositivebutton (R.STRING.ALERT_DIALOG_OK, ... ) . Setnegativebutton (R.string.alert_dialog_cancel, ... ) . Create ();}
This is after the change:
@Overridepublic Dialog Oncreatedialog (Bundle savedinstancestate) { //inflater instantiates View v in the preceding constructor = Inflater.inflate (r.layout.dialog,null); Imagegridview = (GridView) V.findviewbyid (r.id.gridviewimage); Custom view,bla~bla~ Alertdialog.builder Builder = new Alertdialog.builder (getactivity ()); return Builder.setview (v) . SetIcon (Android. R.drawable.ic_dialog_alert) . Settitle (title). setcancelable (False) . Setpositivebutton ( R.STRING.ALERT_DIALOG_OK, ... ) . Setnegativebutton (R.string.alert_dialog_cancel, ... ) . Create ();}
Also at:http://www.barryzhang.com/archives/396
Android: Custom dialogfragment content and buttons