如何使单行 EditText 在使用特定尺寸时正确绘制自身
我需要以编程方式设置 EditText 小部件的高度和宽度。我正在使用相对布局来执行此操作,并且就大小而言它工作正常,但实际的小部件无法正确绘制自身:
我希望它会像这样绘制(如果我将高度设置为 ViewGroup.LayoutParams.WRAP_CONTENT 而不是我请求的高度 - 有时会出现这种情况):
有什么想法吗?
请注意,我无法切换到DIP:s(dpi 独立性实际上已经由使用此代码的引擎处理)。另请注意,我还没有找到任何可以修复错误行为的 setTextSize() 值。
这就是我创建小部件的方式:
EditText et = new EditText(this);
et.setFocusable(true);
et.setSingleLine(true);
et.setHint(label);
et.setText(text);
et.setTextSize(TypedValue.COMPLEX_UNIT_PX,(h/3)+5);
et.setId(cn);
rllp = new RelativeLayout.LayoutParams(w,h);
rllp.leftMargin = x;
rllp.topMargin = y;
layout.addView(et,rllp);
I need to set the height and width of a EditText widget programmatically. I am using a RelativeLayout to do this, and it works fine as far as the size goes, but the actual widget paints itself incorrectly:
I would have expected it to paint like this (which it will if I set the height to ViewGroup.LayoutParams.WRAP_CONTENT instead of my requested height - sometimes):
Any ideas?
Please note that I can't switch to DIP:s (dpi independence is actually already handled by the engine that use this code). Also note that I haven't found any value of setTextSize() that fix the misbehaviour.
This is how I create my widget:
EditText et = new EditText(this);
et.setFocusable(true);
et.setSingleLine(true);
et.setHint(label);
et.setText(text);
et.setTextSize(TypedValue.COMPLEX_UNIT_PX,(h/3)+5);
et.setId(cn);
rllp = new RelativeLayout.LayoutParams(w,h);
rllp.leftMargin = x;
rllp.topMargin = y;
layout.addView(et,rllp);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将发布的代码的最后一行更改为此
try to change the last line of the posted code to this