我想使用 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 是我想用来创建警报对话框的布局。我可以将警报对话框 View 设置为此内部布局吗?
here是图像的 url 。你可以看到我想要使用的布局。
最佳答案
我真的不知道你希望你的对话框看起来像什么,但你应该像这样创建它:
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();
请注意,您尝试膨胀的不是布局,而是本应膨胀的 View (R.id.optionsmenu) (R.layout.activity_main)
alertDialog.builder 用于在不使用 xml 布局的情况下创建对话框
关于android - 使用 xml 的内部布局创建 AlertDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25613495/