2012-02-19 31 views
5

Tôi đang tìm cách thay đổi kích thước (vì vậy nó không chạm vào các cạnh) một bên là EditText trong số AlertDialog.Thay đổi kích thước EditText bên trong một AlertDialog

enter image description here

mẫu mã của tôi

AlertDialog.Builder alert = new AlertDialog.Builder(myActivity.this); 

alert.setTitle("Test"); 

EditText textBox = new EditText(myActivity.this); 
alert.setView(textBox); 

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

Tôi đã thử các giải pháp cung cấp ở đây không có thành công:

Trả lời

22

thêm edittext của bạn vào trong linearlayout và đặt lề thành bố cục đó. xem bên dưới:

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.FILL_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

Cảm ơn bạn đã dành thời gian, điều này đã giúp đưa đúng phần vào đúng vị trí. – ThePaul

Các vấn đề liên quan