安卓:在alertdialog中,EditText和TextView错位。

问题描述 投票:0回答:1

在这段代码中,我正在做一个 AlertDialog 带属性标题。EditText, TextView, 取消 Button并发邮件给我 Button.

EditTextTextView 未正确对齐。

// 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:

Here is an output of my actual code

java android android-alertdialog
1个回答
1
投票

试试吧。

linearLayout.setOrientation(LinearLayout.VERTICAL);

得到正确的方向!

© www.soinside.com 2019 - 2024. All rights reserved.