简体   繁体   English

如何在Android中以编程方式设置EditText框的高度和宽度

[英]How to set EditText box height and width programmatically in Android

I want to manage the height and width of an EditText box programmatically in Android. 我想在Android中以编程方式管理EditText框的高度和宽度。 I tried edittext.setWidth(32); 我试过edittext.setWidth(32); and edittext.setEms(50); edittext.setEms(50); , but both are not working. ,但两者都不起作用。 See my below code, as I am using it to create dynamic EditText s in Android. 请参阅下面的代码,因为我正在使用它在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;
}
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. 试试这个。

edittext.getLayoutParams().width=32;
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)));
RelativeLayout.LayoutParams.width = 32; 
RelativeLayout.LayoutParams.height = 50;

works for me. 适合我。 for example 例如

    lparams.width = 32;
    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) edittext.getLayoutParams();
            params.width = mScreenWidth / 5;
            params.height = mScreenWidth / 5;
            edittext.setLayoutParams(params);

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM