How to set EditText box height and width programma

2019-01-23 13:32发布

I want to manage the height and width of an EditText box programmatically in Android. I tried edittext.setWidth(32); and edittext.setEms(50);, but both are not working. See my below code, as I am using it to create dynamic EditTexts in Android.

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;
}

6条回答
仙女界的扛把子
2楼-- · 2019-01-23 13:50
RelativeLayout.LayoutParams.width = 32; 
RelativeLayout.LayoutParams.height = 50;

works for me. for example

    lparams.width = 32;
查看更多
姐就是有狂的资本
3楼-- · 2019-01-23 13:56
private EditText createEditText()
{
    final LayoutParams lparams = new LayoutParams(50,30); // Width , height
    final EditText edittext = new EditText(this);
    edittext.setLayoutParams(lparams);
    return edittext;
}

Try this.

查看更多
对你真心纯属浪费
4楼-- · 2019-01-23 13:57

Try the following code:-

Display display = getWindowManager().getDefaultDisplay(); 
    int screenWidth = display.getWidth();
    int screenHeight = display.getHeight();

profile_pic.getLayoutParams().height=(screenHeight/6);
    profile_pic.getLayoutParams().width=(screenHeight/6);
查看更多
Deceive 欺骗
5楼-- · 2019-01-23 13:58
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)));
查看更多
一夜七次
6楼-- · 2019-01-23 14:05
edittext.getLayoutParams().width=32;
查看更多
冷血范
7楼-- · 2019-01-23 14:13
    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) edittext.getLayoutParams();
            params.width = mScreenWidth / 5;
            params.height = mScreenWidth / 5;
            edittext.setLayoutParams(params);
查看更多
登录 后发表回答