微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – 如何在对话框中将layout params设置为edittext?

我试图在对话框中为一个edittext设置一些布局参数,但它似乎没有任何效果.为什么?如何减小编辑文本的宽度?

AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder = new AlertDialog.Builder(context);
final EditText input = new EditText(context);                    
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(20, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(10, 0, 10, 0);  
input.setLayoutParams(lp); 

builder.setView(input)
.setCancelable(false)                   
.setPositiveButton(res.getString(R.string.ok), new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
             //...   
    }
})
.setNegativeButton(res.getString(R.string.cancel), new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
        dialog.cancel();
    }
});

解决方法:

你需要在dialog.show()之后设置布局参数,并将布局参数更改为FrameLayout.LayoutParams

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐