开发者

AlertDialog setContentView taking over screen

Perhaps I'm missing something obvious here, but I'm having a hard time setting a custom view for the body of an AlertDialog. Here's what I'm doing to set the custom view:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(View.inflate(getContext(), R.layout.dialog_body, null));
}

Instead of setting the content of the body of the AlertDialog, the view is placed over the enti开发者_如何学Pythonre screen. How do I get the view just to replace the AlertDialog message body?


You are setting the activity's view by calling setContentView which is why it takes up the entire screen. You are also doing it in the onCreate method of what I assume is the activity, you need to do it in the onCreateDialog method.

Heres a link to the docs and an example.

public Dialog onCreateDialog(int id) {

            Dialog dialog = null;
            AlertDialog.Builder builder = new AlertDialog.Builder(app);
            AlertDialog alert = null;

            builder.setTitle("A title") 
                   .setCancelable(true)
                   .setView(myView);
                alert = builder.create();
                return alert;

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜