Изменение размера EditText внутри AlertDialog - PullRequest
5 голосов
/ 19 февраля 2012

Я ищу способ изменить размер (чтобы он не касался краев) EditText внутри AlertDialog.

enter image description here

Мой пример кода

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();

Я пробовал решения, представленные здесь, безуспешно:

1 Ответ

23 голосов
/ 19 февраля 2012

добавьте ваш текст редактирования внутри линейного макета и установите поля для этого макета. см. ниже:

    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.FILL_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();
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...