8

我尝试使用 LayoutParams.MATCHPARENT 为其宽度和高度动态创建一个编辑文本。我已经尝试了以下代码,但它仍然无法正常工作。

EditText editor = ....;
LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1);
editor.setLayoutParams(params);
editor.setGravity(Gravity.LEFT | Gravity.TOP);
editor.setBackgroundColor(Color.WHITE);
editor.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI);
editor.setTextColor(Color.BLACK);
//editor.setSingleLine(false);
editor.setFocusableInTouchMode(true);
editor.setInputType(EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE | EditorInfo.TYPE_TEXT_FLAG_IME_MULTI_LINE);
editor.setMaxLines(Integer.MAX_VALUE);
editor.setHorizontallyScrolling(false);
editor.setTransformationMethod(null);

上面的代码让我输入了几行,键盘上会显示一个“ENTER”按钮,但是当我在编辑文本上键入时,如果宽度不足以显示文本,编辑文本会水平滚动。有人可以帮我如何制作edittext多行吗?

4

4 回答 4

8

试试下面的代码

editor.setSingleLine(false);
editor.setHorizontalScrollBarEnabled(false);
于 2012-07-31T12:52:36.957 回答
5

试试这个代码

    txt.setSingleLine(false);
    txt.setLines(14);
    txt.setMinLines(13);
    txt.setMaxLines(15);
    txt.setGravity(Gravity.LEFT | Gravity.TOP);
于 2013-09-27T08:15:52.787 回答
1

尝试

android:scrollHorizontally="false"

对于编辑文本

于 2012-07-31T12:48:48.517 回答
1

将以下行添加到您的代码中,它可能会对您有所帮助。

editor.setSingleLine(false);
于 2012-07-31T12:55:49.557 回答