Android:EditText 和 TextView 在警报对话框中未对齐

Android : EditText and TextView are misaligned in alertdialog

在这段代码中,我正在制作一个 AlertDialog,其中包含属性标题、EditTextTextView、取消 Button 和电子邮件给我 Button

EditTextTextView 不 aligned/set 正确。

// Alert Dialog
private void showForgotpasswdDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Forgot your password?");
    // Set linear layout
    LinearLayout linearLayout = new LinearLayout(this);
    // View to set an dialog
    final EditText Email = new EditText(this);
    Email.setHint("Email");
    Email.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
    // Text view
    linearLayout.addView(Email);
    builder.setView(linearLayout);
    // Text view
    final TextView tv = new TextView(this);
    tv.setText("Unfortunately, if you have never given us your email, we will not be able to reset your password");
    linearLayout.addView(tv);
    builder.setView(linearLayout);
    // Buttons for EMAIL ME
    builder.setPositiveButton("EMAIL ME", new 
    DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            // Input email
            String email = Email.getText().toString().trim();
            beginforgotpasswd(email);
        }
    });

    // Buttons for CANCEL
    builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
         @Override
         public void onClick(DialogInterface dialog, int i) {
             // Dismiss dialog
             dialog.dismiss();
         }
    });

     // Show dialog
     builder.create().show();
}

请查看以下显示未对齐 EditText 的屏幕截图:

试试吧:

linearLayout.setOrientation(LinearLayout.VERTICAL);

获得正确的方向!