【问题标题】:Resizing EditText inside of an AlertDialog在 AlertDialog 内调整 EditText 的大小
【发布时间】:2012-03-09 21:14:16
【问题描述】:

我正在寻找一种方法来调整 EditText 内部的 AlertDialog 的大小(使其不会触及边缘)。

我的示例代码

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

我尝试了这里提供的解决方案,但没有成功:

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    LinearLayout 中添加您的EditText 并为该布局设置边距。

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

    【讨论】:

    • 感谢您抽出宝贵时间,这有助于将正确的部分放在正确的位置。
    • 对我来说,这让 editText 看起来更好 -> params.setMargins(55, 0, 55, 0);
    猜你喜欢
    • 2019-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多