Resizing EditText inside of an AlertDialog

前端 未结 1 1902
南方客
南方客 2020-12-21 01:39

I\'m looking for a way to resize (so it doesn\'t touch the edges) an EditText inside of an AlertDialog.

My sample code

相关标签:
1条回答
  • 2020-12-21 02:02

    Add your EditText inside a LinearLayout and set margins to that layout.

    AlertDialog.Builder alert = new AlertDialog.Builder(this);
    
    alert.setTitle("Test");
    
    LinearLayout layout = new LinearLayout(this);
    layout.setOrientation(LinearLayout.VERTICAL);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
         LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    params.setMargins(20, 0, 30, 0);
    
    EditText textBox = new EditText(myActivity.this);
    layout.addView(textBox, params);
    
    alert.setView(layout);
    
    alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
        // do nothing 
        }
    });
    
    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
    // do nothing
    }
    });
    
    alert.show();
    
    0 讨论(0)
提交回复
热议问题