在对话框中查看太小,截断文本

View in Dialog too small, cutting off text

我创建了一个对话框,其中显示文本的视图似乎比对话框小,导致文本被截断。
我做错了什么?

String result = "This is line 1 11111111111111111111111\n" + "This is line 2 22222222222222222\n" + "This is line 3 3333333333333333333333\n" +
    "This is line 4 44444444444444444444444\n" + "This is line 5 55555555555555555\n" + "This is line 6 6666666666666666666666\n" +
    "This is line 7 77777777777777777777777\n" + "This is line 8 88888888888888888\n" + "This is line 9 9999999999999999999999\n" +
    "This is line 1010101010101010101010101\n" + "This is line 11 1111111111111111\n" + "This is line 12 1212121212121212121212\n";

Display display = getActivity().getWindowManager().getDefaultDisplay();
int displayWidth = display.getWidth();
int displayHeight = display.getHeight();

AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder. setPositiveButton("Dismiss",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog,int which) {
                    }
                }).setIcon(android.R.drawable.ic_dialog_info);
builder.setMessage(result);
Dialog dialog = builder.setView(new View(mContext)).create(); 
dialog.setTitle("Details");

dialog.show();

WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
layoutParams.copyFrom(dialog.getWindow().getAttributes());
layoutParams.width = displayWidth - 50;
layoutParams.height = displayHeight - 100;

// change position of window on screen
layoutParams.x = displayWidth / 2;     
layoutParams.y = (displayHeight / 2);  

layoutParams.dimAmount = 0.7f; //change this value for more or less dimming

dialog.getWindow().setAttributes(layoutParams);

dialog.getWindow().addFlags (
        WindowManager.LayoutParams.FLAG_DIM_BEHIND |
        WindowManager.LayoutParams.FLAG_FULLSCREEN);

我不明白你在用这些代码做什么(看起来像是随机的代码行),但可以肯定的是代码会按预期工作:

AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setPositiveButton("Dismiss",
                new DialogInterface.OnClickListener() {
                    @Override 
                    public void onClick(DialogInterface dialog,int which) {
                    } 
                }).setIcon(android.R.drawable.ic_dialog_info);
builder.setMessage(result);
builder.setTilte("Details");
builder.create().show(); 

问题出在您在 alertDialog 中实现的添加的自定义视图

setView(new View(mContext))

将会发生的是,它将有一个 space 放置视图的位置,导致内联消息由于太长而被截断。

只需删除它或创建一个 xml 布局,在其中将其设置为 alertDialog 的自定义视图。

AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder. setPositiveButton("Dismiss",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog,int which) {
                }
            }).setIcon(android.R.drawable.ic_dialog_info);

    builder.setView(//your xml view here);

    Dialog dialog = builder.create();

    TextView message = dialog.findViewById(//the id of the textview);
    message.setText(result);

    dialog.setTitle("Details");

    dialog.show();