繁体   English   中英

用视图(按钮)覆盖Android AlertDialog

[英]Overlay Android AlertDialog with a View (Button)

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

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

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

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

您可以在此处获得有关如何实现自定义视图的更多信息。

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

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 您不能在AlertDialogAlertDialog视图用作子视图

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM