安卓 - 编辑文本多行强制填充高度



我需要一个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,一旦我在多行上键入返回按钮写了一些东西,它就会更长且可滚动。我知道我可以设置行号,但在这种情况下,它应该适用于不同的设备,并且每个设备(我猜)都有不同数量的行。

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

有什么提示吗?

尝试使用(没有 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" />

尝试为 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"
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()); <--------

将在所有设备中工作。

希望这对您有所帮助。

我的代码对我来说是工作。

<?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>

如果您可以在代码中添加它,它将如下所示:

EditText et = new EditText(this);
l.addView(et, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 100));

其中100意味着它将具有 100dp 的宽度。 l是您可能拥有的LayoutScrollView在您的情况下),您可以使用R.layout.name_of_the_scroll_view获得。

我不确定,但尝试将WRAP_CONTENT的高度属性放入

.

最新更新