android - 使用 xml 的内部布局创建 AlertDialog

标签 android layout dialog android-alertdialog

我想使用 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/

相关文章:

c# - ListBox 知道它在某个遥远的 ScrollViewer 中吗?

javascript - 如何更改 jQuery UI 对话框的背景颜色?

android - 哪个更好地为 ListView 加载图像?

javascript - Cordova 3.5.0 上的 SQLite 插件无法正常工作

android - 每天更新 Android 应用程序?

python - matplotlib 中的 Widget 可以自动定位吗?

Java - 尝试规划程序类的布局

java - TitleAreaDialog - 调整标题图像

flutter - 在 AlertDialog 中获取复选框

android - 广播和接收额外的 double