我想在Android中以编程方式管理EditText
框的高度和宽度。我尝试了edittext.setWidth(32);
和edittext.setEms(50);
,但两者都无效。请参阅下面的代码,因为我正在使用它在Android中创建动态EditText
。
private EditText createEditText()
{
final LayoutParams lparams = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
final EditText edittext = new EditText(this);
edittext.setLayoutParams(lparams);
edittext.setWidth(32);
edittext.setEms(50);
return edittext;
}
答案 0 :(得分:26)
private EditText createEditText()
{
final LayoutParams lparams = new LayoutParams(50,30); // Width , height
final EditText edittext = new EditText(this);
edittext.setLayoutParams(lparams);
return edittext;
}
试试这个。
答案 1 :(得分:12)
edittext.getLayoutParams().width=32;
答案 2 :(得分:6)
DisplayMetrics metrics = new DisplayMetrics();
WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
wm.getDefaultDisplay().getMetrics(metrics);
final float height = metrics.heightPixels;
EditText edittext = new EditText(this);
edittext.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,(int) (height/2)));
答案 3 :(得分:1)
RelativeLayout.LayoutParams.width = 32;
RelativeLayout.LayoutParams.height = 50;
适合我。例如
lparams.width = 32;
答案 4 :(得分:0)
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) edittext.getLayoutParams();
params.width = mScreenWidth / 5;
params.height = mScreenWidth / 5;
edittext.setLayoutParams(params);
答案 5 :(得分:-2)
请尝试以下代码: -
Display display = getWindowManager().getDefaultDisplay();
int screenWidth = display.getWidth();
int screenHeight = display.getHeight();
profile_pic.getLayoutParams().height=(screenHeight/6);
profile_pic.getLayoutParams().width=(screenHeight/6);