Android - EditText多行强制填充高度

5
我需要一个填充窗口高度的EditText。 我正在使用以下代码:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true" >

<EditText
    android:id="@+id/text_editor"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:background="#ff0000"
    android:gravity="top|left"
    android:inputType="textMultiLine"
    android:textColor="@android:color/white" />

</ScrollView>

但是我得到的只是一个单行EditText,一旦我使用多行输入并按下回车键,它就会变得更长并可滚动。我知道我可以设置行数,但在这种情况下,它应该适用于不同的设备,每个设备(我猜)具有不同数量的行。

我如何强制它填充设备高度?

有什么提示吗?

5个回答

4

尝试不使用ScrollView:

<EditText
    android:id="@+id/text_editor"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:background="#ff0000"
    android:gravity="top|left"
    android:textColor="@android:color/white" />

没有使用ScrollView确实有所帮助。 - Monica M

3
尝试将此设置用于EditText,我遇到了相同的问题,这将有效。
<EditText
android:id="@+id/text_editor"
android:layout_width="match_parent"
**android:layout_height="wrap_content"**
android:background="#ff0000"
android:gravity="top|left"
android:inputType="textMultiLine"
android:textColor="@android:color/white" />

你也可以使用这个功能来精确设置所需的行数,

android:lines="number of Lines"

0
如果您可以将其添加到代码中,它会像这样:
EditText et = new EditText(this);
l.addView(et, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 100));

其中100表示它将具有100dp的宽度。 l是您可能拥有的Layout(在您的情况下为ScrollView),您可以使用R.layout.name_of_the_scroll_view获取。

我不确定,但尝试将高度属性设置为WRAP_CONTENT


0
RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <EditText
        android:id="@+id/editText1"
        android:scrollbars="vertical" <-----------
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:gravity="top|left"
        android:ems="10"
        android:text=" example text" />

</RelativeLayout>

在Java代码中设置这样.....
ed1 = (EditText) findViewById(R.id.editText1);
ed1.setMovementMethod(new ScrollingMovementMethod()); <--------

将适用于所有设备。

希望这能帮到你。


0
我的代码对我来说运行正常。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical" >

<ImageView
    android:id="@+id/imageView20"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:contentDescription="@string/img_desc1"
    android:src="@drawable/num74_head_payakorn" />

<TableRow
    android:id="@+id/tableRow1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <EditText
        android:id="@+id/edtPayakorn"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:textSize="22sp"
        android:clickable="false"
        android:cursorVisible="false"
        android:editable="false"
        android:enabled="true"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:gravity="top|left"
        android:scrollbars="vertical"
        android:inputType="textMultiLine" />
    </TableRow>
</LinearLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接