我以编程方式添加editText控件,控件的值比控件的宽度长,并且包装到下一行。我希望文本水平读取而不跨越多行或允许垂直滚动
我尝试了以下设置的多种组合,但它们似乎根本没有影响它。
et.setHorizontallyScrolling(true);
et.setSingleLine(true);
et.setLines(1);
et.setMaxLines(1);
et.setHorizontalScrollBarEnabled(true);
et.setRawInputType(InputType.TYPE_CLASS_TEXT| InputType.TYPE_TEXT_FLAG_IME_MULTI_LINE);
删除所有,只使用et.setSingleLine(true);
只需更改您的代码....借助于此
public class MainActivity extends Activity {
private LinearLayout ll;
private ScrollView sv;
private EditText et,et1,et2,et3;
@Overrid
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
ll.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
et = new EditText(this);
et.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
et.setHeight(80);
sv = new ScrollView(this);
sv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
sv.addView(et);
ll.addView(sv);
setContentView(ll);
}
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
ll.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
EditText et = new EditText(this);
et.setSingleLine(true);
et.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT));
et.setRawInputType(InputType.TYPE_CLASS_TEXT| InputType.TYPE_TEXT_FLAG_IME_MULTI_LINE);
ll.addView(et);
setContentView(ll);
}
只使用setSingleLine(true);
这很好用。