Java 是否更改alertBox中显示的EditText(任何视图)的大小?

Java 是否更改alertBox中显示的EditText(任何视图)的大小?,java,android,android-alertdialog,Java,Android,Android Alertdialog,我想显示一个用于用户输入的alertBox。所以我在alertBox中添加了EditText视图。我的alertBox的代码是: final EditText input = new EditText(this); //input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); input.setX(10);

我想显示一个用于用户输入的alertBox。所以我在alertBox中添加了EditText视图。我的alertBox的代码是:

final EditText input = new EditText(this);
            //input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
            input.setX(10);
            android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(this);
            builder.setMessage("Title")
                    .setCancelable(false)
                    .setView(input)
                    .setTitle("Create new Playlist")
                    .setPositiveButton("CREATE",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    // finish the current activity
                                    // AlertBoxAdvance.this.finish();
                                    playlistName = input.getText().toString();
                                    dialog.cancel();


                                }
                            })
                    .setNegativeButton("CANCEL",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    // cancel the dialog box
                                    dialog.cancel();
                                }
                            });
            android.app.AlertDialog alert = builder.create();
            alert.show();
这将提供以下警告对话框:

但EditText不是从标题和警报消息开始的位置开始的(未正确缩进)。所以我想要以下类型的EditText,它在alert对话中有适当的缩进

如何做到这一点?如何更改警报对话框中视图的位置?或者如何更改视图大小?

尝试以下操作:

XML布局:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    android:orientation="vertical">

    <EditText
        android:id="@+id/editTextField"
        android:layout_marginLeft="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="text" />

</LinearLayout>

您可以尝试使用自定义视图。。。 在布局文件中定义EditText,例如lay.xml

然后


请尝试使用自定义视图。您能举个例子吗?请看以下文档:@14bce109:在将其传递给用户之前,为
input
视图设置布局和边距setView@14bce109,请检查我的答案以获取示例代码
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mActivity);
    LayoutInflater inflater = mActivity.getLayoutInflater();
    final View dialogView = inflater.inflate(R.layout.dialog, null);
    dialogBuilder.setView(dialogView);

final EditText mEditText = (EditText) dialogView.findViewById(R.id.editTextField);

dialogBuilder.setTitle("Title");
dialogBuilder.setMessage("Type your message here");

dialogBuilder.setPositiveButton("Yes", null);
dialogBuilder.setNegativeButton("No", null);

final AlertDialog alertDialog = dialogBuilder.create();
alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {

    @Override
    public void onShow(DialogInterface dialog) {
        Button positiveButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
        positiveButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                String enteredTextString= mEditText.getText().toString();
                //To whatever with the text entered
            }
        });
        Button negativeButton = alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE);
        negativeButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                alertDialog.dismiss();
            }
        });
    }
});
alertDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
    @Override
    public void onDismiss(DialogInterface dialog) {

    }
});
alertDialog.show();
builder.setMessage("Title")
                .setCancelable(false)
                .setContentView(R.layout.lay);