Как это ..
Создайте свой XML-макет
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dialog_layout_root"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
>
И тогда вы можете установить макет на компоновщике с помощью следующего:
LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.dialog_layout, (ViewGroup) findViewById(R.id.dialog_layout_root));
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(dialoglayout);
EDIT:
Вы должны изменить свой код на что-то вроде этого ...
Создайте AlertDialog.Builder на уровне класса.
private AlertDialog.Builder builder;
В вашем onCreate () создайте свой AlertDialog
LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.dialog_layout, (ViewGroup) findViewById(R.id.dialog_layout_root));
//Ask the user if they want to quit
builder
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle(R.string.quit)
.setMessage(R.string.really_quit)
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//Stop the activity and pause media player
mainSound.pause();
MainActivity.this.finish();
}
})
.setNegativeButton(R.string.no, null)
.setView(dailogLayout);
public boolean onKeyDown(int keyCode, KeyEvent event) {
//Handle the back button
if(keyCode == KeyEvent.KEYCODE_BACK) {
builder.show();
return true;
}
else {
return super.onKeyDown(keyCode, event);
}
}