문제

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 entire 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;

}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top