【问题标题】:Create AlertDialog using inside layout of a xml使用 xml 的内部布局创建 AlertDialog
【发布时间】:2014-09-01 22:17:20
【问题描述】:

我想使用 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 是图片的网址。你可以看到我想要使用的布局。

【问题讨论】:

    标签: android layout dialog android-alertdialog


    【解决方案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 布局的情况下创建对话框

    【讨论】:

    • 这将创建具有主要活动视图的对话框。但我只想要内部布局。实际上,我想要 xml 中的布局位置,因为当我使用内部布局时,我不需要调整对话框的大小来放置 orjinal 点。
    猜你喜欢
    • 2013-06-27
    • 1970-01-01
    • 1970-01-01
    • 2021-12-06
    • 1970-01-01
    • 1970-01-01
    • 2015-11-08
    • 2017-09-01
    • 1970-01-01
    相关资源
    最近更新 更多