我想使用xml中的布局创建一个警告对话框。我试过这个:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
View v = inflater.inflate(R.id.optionsmenu, null);
AlertDialog dialog = builder.setView(v).create();
dialog.show();
它不起作用。 optionsmenu是我想用来创建警告对话框的布局。我可以将警报对话框视图设置为此内部布局吗?
here是图片的网址。您可以看到我想要使用的布局。
答案 0 :(得分:1)
我真的不知道您希望对话框看起来如何,但您应该像这样创建它:
Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.activity_main);
dialog.setTitle("");
//to use a view inside the xml (i.e. a button)
Button button = (Button)dialog.findViewById(R.id.optionsmenu);
dialog.show();
请注意,您试图不是对布局进行充气,而是尝试对它进行充气(R.id.optionsmenu)(R.layout.activity_main)
alertDialog.builder用于创建不使用xml布局的对话框