简体   繁体   中英

Overlay Android AlertDialog with a View (Button)

I have created an AlertDialog (like below) and would like to display a View (floating action button from library) over it.

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

It currently looks like this, but the dialog overlays the button. I want the button to be above the AlertDialog and clickable as well. Android对话框叠加层 My FloatingActonButton object is called "fab".

What you need to do is to create your own custom view and attach it in your AlertDialog to have full control of the layout of the dialog itself

You can go here for more info on how to implement a custom view.

You have to make a custom layout for dialog in order to include buttons of your choice. Below is the example of how you can make it work:

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

-EDIT-

Please note that the above example is an alternative to AlertDialog . You cannot use this as a child view in AlertDialog

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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