【问题标题】:Overlay Android AlertDialog with a View (Button)使用视图(按钮)覆盖 Android AlertDialog
【发布时间】:2015-07-20 15:54:27
【问题描述】:

我创建了一个 AlertDialog(如下所示)并希望在其上显示一个视图(库中的浮动操作按钮)。

new AlertDialog.Builder(getActivity())
    .setTitle("Last Day of School Description")
.setMessage(...)
.show();

目前看起来像这样,但对话框覆盖了按钮。我希望按钮位于 AlertDialog 上方并且也可以单击。 我的 FloatingActonButton 对象被称为“fab”。

【问题讨论】:

  • 您需要在警报对话框中自定义视图
  • 你能提供一个例子的链接或者我可以开始的地方吗?
  • @apoorvk 如果我的回答有帮助,您至少可以投票吗?

标签: java android button android-alertdialog material-design


【解决方案1】:

您需要做的是创建自己的自定义视图并将其附加到您的AlertDialog 以完全控制对话框本身的布局

您可以前往here 了解有关如何实现自定义视图的更多信息。

【讨论】:

    【解决方案2】:

    您必须为对话框制作自定义布局才能包含您选择的按钮。以下是如何使其工作的示例:

    public void showCustomDialog(){
     try{
        final Dialog d = new Dialog(act);
        d.setContentView(R.layout.custom_layout_fordialog); // your custom dialog layout
        d.setCancelable(false);
    
    
        TextView status = (TextView) d.findViewById(R.id.result_status);
        status.setText("Title of dialog");
    
        TextView msg = (TextView) d.findViewById(R.id.result_msg);
        msg.setText("body of dialog");
    
        Button ok = (Button) d.findViewById(R.id.result_ok); // your button
    
    
    
        ok.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // do whatever you want here
    
            }
        });
        d.show();
        }
        catch(Exception e){
            e.printStackTrace();
        }
    
    } // method end
    

    -编辑-

    请注意,上面的示例是 AlertDialog 的替代方法。您不能在AlertDialog中将其用作子视图

    【讨论】:

    • 这不起作用,我的结果是对话框内的按钮,而不是右下角的按钮。
    • @apoorvk 请根据您希望的对话框自行设计布局。你不能用 XML 设计一个布局吗?然后在d.setContentView(R.layout.YOUR_LAYOUT_FOR_DIALOG)中设置布局,后面没有火箭科学。
    • 我已经这样做了,但我的问题是您无法在父视图之外创建子视图(对话框)。你知道有什么替代方法可以完成这项工作吗?
    • @apoorvk 我上面的解决方案不希望你在AlertDialog 中做这个。它是整个AlertDialog 的替代方案,只需删除AlertDialog 代码并改为调用Dialog……在我的情况下,它可以按我的意愿工作,我将垂直按钮设置为我的自定义布局..
    猜你喜欢
    • 1970-01-01
    • 2019-03-22
    • 2016-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多