简体   繁体   中英

Android : EditText and TextView are misaligned in alertdialog

In this code, I am making an AlertDialog with attributes title, EditText , TextView , Cancel Button , and Email me Button .

EditText and TextView not aligned/set properly.

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

Please see the following screenshot showing the misaligned EditText :

这是我的实际代码的输出

just try it:

linearLayout.setOrientation(LinearLayout.VERTICAL);

to get proper orientation!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM