Изменение размера предупреждения - PullRequest
0 голосов
/ 11 октября 2018

У меня есть AlertDialog, который предлагает пользователю ввести текст в EditText, но всплывающее окно AlertDialog не так широко, как мне нужно.Вот как это выглядит: enter image description here

Как мне сделать его шире?

Это соответствующая часть моего кода:

  AlertDialog.Builder alert = new AlertDialog.Builder(getContext(), R.style.AlertDialogCustom);
  // Using the following constructor produces a desirable width but 
  // the wrong theme colors
  // AlertDialog.Builder alert = new AlertDialog.Builder(getContext());

  final EditText edittext = new EditText(getContext());
  alert.setMessage("Enter Your Message");
  alert.setTitle("Enter Your Title");

  alert.setView(edittext);

  alert.setPositiveButton("Yes Option", new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int whichButton) {

    String YouEditTextValue = edittext.getText().toString();
   }
  });

  alert.setNegativeButton("No Option", new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int whichButton) {
    // what ever you want to do with No option.
   }
  });

  alert.show();

Моя попытка темы:

<style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog">
    <item name="android:textColor">@color/colorLightGray</item>
    <item name="android:typeface">monospace</item>
    <item name="android:textSize">10sp</item>
    <item name="android:windowBackground">@color/colorDarkerGray</item>
</style>

Ответы [ 2 ]

0 голосов
/ 11 октября 2018

Один раз попробуйте это

 // Using the following constructor produces a desirable width but
        // the wrong theme colors
        // AlertDialog.Builder alert = new AlertDialog.Builder(getContext());

        AlertDialog alertDialog =  new AlertDialog.Builder(this, R.style.AlertDialogCustom).create();

        final EditText edittext = new EditText(this);
        alertDialog.setMessage("Enter Your Message");
        alertDialog.setTitle("Enter Your Title");

        alertDialog.setView(edittext);
        alertDialog.setButton(AlertDialog.BUTTON_POSITIVE,"Yes Option", new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int whichButton) {

                String YouEditTextValue = edittext.getText().toString();
            }
        });

        alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE,"No Option", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                // what ever you want to do with No option.
            }
        });

        alertDialog.show();

        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();

        lp.copyFrom(alertDialog.getWindow().getAttributes());
        lp.width = WindowManager.LayoutParams.MATCH_PARENT; // or you can give fix size lp.width = 500
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        alertDialog.getWindow().setAttributes(lp);
0 голосов
/ 11 октября 2018

Установите настраиваемые параметры компоновки Windowmanager в соответствии с вашими потребностями, и вы должны быть готовы к работе.Сделайте это после вызова диалогового окна show(), например:

alertDialog.show();
WindowManager.LayoutParams layoutParams = new 
WindowManager.LayoutParams();

layoutParams .copyFrom(alertDialog.getWindow().getAttributes());
layoutParams .width = 150;
layoutParams .height = 500;
layoutParams .x=-170;
layoutParams .y=100;
alertDialog.getWindow().setAttributes(layoutParams );
...