How to create dialog which will be full in horizontal dimension

前端 未结 2 750
你的背包
你的背包 2020-12-13 06:34

When I use in layout, which specifies this dialog android:layout_width=\"match_parent\" I get this dialog:

相关标签:
2条回答
  • 2020-12-13 06:49

    You can do that by grabbing the Window object that the dialog uses, and resetting the width. Here's a simple example:

    //show the dialog first
    AlertDialog dialog = new AlertDialog.Builder(this)
            .setTitle("Test Dialog")
            .setMessage("This should expand to the full width")
            .show();
    //Grab the window of the dialog, and change the width
    WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
    Window window = dialog.getWindow();
    lp.copyFrom(window.getAttributes());
    //This makes the dialog take up the full width
    lp.width = WindowManager.LayoutParams.MATCH_PARENT;
    lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
    window.setAttributes(lp);
    

    Here's the end result. Note that there's a lot more styling you can do to the dialog if you need to fine tune things (like change the background, etc). When I've had to do this in the past, I usually use this method, and customize the layout used in the dialog with setView() on the builder class.

    example

    0 讨论(0)
  • 2020-12-13 06:58

    Another way to fix this is to use:

    import android.support.v7.app.AlertDialog;
    

    instead of:

    import android.app.AlertDialog;
    
    0 讨论(0)
提交回复
热议问题