简体   繁体   English

如何以编程方式在EditText上设置大小

[英]How set size on an EditText programmatically

I have created an EditText programmatically. 我已经以编程方式创建了一个EditText。 This EditText has one line and is added to a LinearLayout: 此EditText只有一行,并添加到LinearLayout中:

LinearLayout ll = (LinearLayout) myView.findViewById(R.id.select_options_option);
v = new EditText(context);
((EditText) v).setLines(1);
ll.addView(v);

My problem is that I can only type one character in this EditText. 我的问题是我只能在此EditText中键入一个字符。 How can I set the width of an EditText, so that I can type in 3 or more charaters? 如何设置EditText的宽度,以便可以输入3个或更多字符?

Try adding layout parameters to the linear layout before adding the TextView 在添加TextView之前,尝试将布局参数添加到线性布局

LinearLayout ll = (LinearLayout) myView.findViewById(R.id.select_options_option);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ll.setLayoutParams(lp);
v = new EditText(context);
((EditText) v).setLines(1);
ll.addView(v)

You can also add the WRAP_CONTENT in your xml file 您也可以在xml文件中添加WRAP_CONTENT

If you want your edittext to match the width of linearLayout 如果您希望您的edittext匹配linearLayout的宽度

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                                         LinearLayout.LayoutParams.WRAP_CONTENT);
v.setLayoutParams(lp);

other wise if you want width to be equal to few character wide.. 否则,如果您希望宽度等于几个字符的宽度。

see http://developer.android.com/reference/android/widget/TextView.html#setEms(int) 参见http://developer.android.com/reference/android/widget/TextView.html#setEms(int)

v.setEms(5);
Try this too you can set EditText box height and width:

 LayoutParams lparams = new LayoutParams(60,30); // Width , height or (LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT)
 EditText edittext = new EditText(this);
 edittext.setLayoutParams(lparams);

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

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