如何在对话框中设置布局参数以编辑文本?
我试图将一些布局参数设置为对话框中的编辑文本,但它似乎没有效果。为什么?如何减少编辑文本的宽度?
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();
}
});
I'm trying to set some layout params to an edittext in a dialog but it seems to have no effect. Why? How can I reduce the width of my 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();
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在
dialog.show()
之后设置布局参数,并将布局参数更改为FrameLayout.LayoutParams
You need to set the layout params after the
dialog.show()
and change the layout params toFrameLayout.LayoutParams