Android EditText in AlertDialog seems too wide

风流意气都作罢 提交于 2020-01-12 06:51:05

问题


It seems like the EditText in the image below is too wide. I assume that I have misused the SDK in some way and until convinced otherwise I am not looking for a way to specify some number of margin/padding pixels on the sides of the EditText.

This one looks more appropriate.

Here's my code (that creates the first, 'Create Tag', dialog):

final Dao<Tag, Integer> tagDao = getHelper().getTagDao();

final EditText input = new EditText(this);
input.setSingleLine(true);
input.setHint(R.string.create_tag_dialog_hint);

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(input);
builder.setTitle(getString(R.string.create_tag_dialog_title));
builder.setPositiveButton(
    getString(R.string.create_tag_dialog_positive),
    new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            String value = input.getText().toString().trim();
            Toast.makeText(getApplicationContext(), value, Toast.LENGTH_SHORT).show();
            Tag tag = new Tag(value);
            try {
                    tagDao.create(tag);
            } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
            }
        }
    });
builder.setNegativeButton(
    getString(R.string.create_tag_dialog_negative), null);
builder.show();

Sorry for the length of the post and thanks for any helpful comments.


回答1:


Just sorted this myself. Using an instance of AlertDialog, you can specify setView and pass in spacing parameters. This will work.

final EditText input = new EditText(this);

AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Title");
alertDialog.setMessage("Message");
alertDialog.setView(input, 10, 0, 10, 0); // 10 spacing, left and right
alertDialog.setButton("OK", new OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        // Clicked
    }
});
alertDialog.show();

Edit: I'm aware this question is old, but no solution was provided.




回答2:


You can do it like this:

LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setGravity(Gravity.CENTER_HORIZONTAL);
final EditText input = new EditText(this);
input.setSingleLine(true);
layout.setPadding(10, 0, 10, 0);
input.setHint("Hint");
layout.addView(input);

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(layout);

Moreover, setSingleLine is deprecated. You should use InputStyle.




回答3:


Set the layout marginleft and layout marginright to 5sp. The margin sets the space around the view group. Take a look at ViewGroup.MarginLayoutParams




回答4:


Try setting some padding on your EditText using the setPadding method. This should add a gap between the edge of the EditText and the border of the dialog. N



来源:https://stackoverflow.com/questions/4927208/android-edittext-in-alertdialog-seems-too-wide

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!