调整AlertDialog内的EditText的大小

The*_*aul 5 android android-layout

我正在寻找一种方式来调整(所以它不会触及边缘)的EditText的内部AlertDialog.

在此输入图像描述

我的示例代码

AlertDialog.Builder alert = new AlertDialog.Builder(myActivity.this);

alert.setTitle("Test");

EditText textBox = new EditText(myActivity.this);
alert.setView(textBox);

alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
    // do nothing 
    }
});

alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
// do nothing
}
});

alert.show();
Run Code Online (Sandbox Code Playgroud)

我试过这里提供的解决方案没有成功:

waq*_*lam 23

在linearlayout中添加您的edittext并将边距设置为该布局.见下文:

AlertDialog.Builder alert = new AlertDialog.Builder(this);

alert.setTitle("Test");

LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
     LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(20, 0, 30, 0);

EditText textBox = new EditText(myActivity.this);
layout.addView(textBox, params);

alert.setView(layout);

alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
    // do nothing 
    }
});

alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
// do nothing
}
});

alert.show();
Run Code Online (Sandbox Code Playgroud)