How to resize an AlertDialog.Builder?

前端 未结 2 1734
迷失自我
迷失自我 2020-12-22 09:57

I\'d like to resize my AlertDialog.Builder but I didn\'t find any resolution on this website, I tried everything I saw on questions like this one but seems olde

相关标签:
2条回答
  • 2020-12-22 10:36

    You should use custom dialog layout, check documentation.

    Actually, you can resize dialog by getDialog().getWindow().setLayout(width, height); just call it onResume (not onCreateView).

    Your view must be nested inside of a ScrollView, check post.

    Reading huge amount of text in a Dialog can be annoying, it's better to use all pixels you have, consider simple Activity/Fragment.

    0 讨论(0)
  • 2020-12-22 10:39

    If you're using an AlertDialog within a DialogFragment, with the AppCompat support library 23.2.1 or above, you must put the resize code in DialogFragment.onStart(), otherwise it will not work. Something like this:

    @Override
    public void onStart() {
        super.onStart();
        // Method 1
        alertDialog.getWindow().getAttributes().width = 400; // pixels
        alertDialog.getWindow().getAttributes().height = 500; // pixels
        // Re-apply the modified attributes
        alertDialog.getWindow().setAttributes(
               alertDialog.getWindow().getAttributes());
    
        // Method 2 (alternative)
        alertDialog.getWindow().setLayout(400, 500); // size in pixels
    }
    

    More info:

    • http://babe.ilandroid.com/alertdialog-with-custom-view-resize-to-wrap-the-views-content.html
    • https://maven.google.com (list of available support library versions for gradle)
    0 讨论(0)
提交回复
热议问题