What is difference between Dialog.setContentView( View ) & AlertDialog.setView( View )

百般思念 提交于 2019-12-08 15:27:01

问题


I'm working on creating a custom dialog to get input from the user. I've noticed that when I use an AlertDialog I can add widgets such as an EditText to the AlertDialog using setView() method. I'm looking into customizing AlertDialog and noticed that Dialog has a method setContentView(). In the subclass of AlertDialog I could use either setContentView() or setView().

One difference I've noticed is when I use setView() on an AlertDialog, I can see Positive & Negative buttons. In my subclass of AlertDialog, using setContentView() I don't get the Positive & Negative buttons.

Can anyone explain other differences?

Thanks.


回答1:


If I undesrstood it correctly, setContentView inflates the entire Dialog window with your custom layout. Title, icon, buttons... nothing remains. setView on the other hand sets the layout of the view between the buttons and the title.




回答2:


setContentView is AlertDialog' father class method, it means all the dialog windows, and setView is AlertController method, in AlertDialog windows view, there are topPanel,contentPanel,buttonPanel.setView only set customView to contentpanel.




回答3:


BTW you can also call the AlertDialogs setView method, which seems to also remove the extra "spacing" around the view.

void setView(View view, int viewSpacingLeft, int viewSpacingTop, int viewSpacingRight, int viewSpacingBottom)

like this.

alertDialog.setView(myView,0,0,0,0);



来源:https://stackoverflow.com/questions/6040883/what-is-difference-between-dialog-setcontentview-view-alertdialog-setview

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