如何固定多行文本编辑框的大小

20

我想创建一个多行编辑文本框,但是问题在于每当我输入新的一行时,编辑文本框的高度都会增加。如何解决它的高度问题。我的代码是:

<EditText
    android:id="@+id/editAdditionalInfo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editPhoneCutomerDetails"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="60dp"
    android:layout_below="@+id/editPhoneCutomerDetails"
    android:layout_marginTop="10dp"
    android:background="@drawable/message_additional_box1x"
    android:ems="10"  
    android:padding="10dp"
    android:gravity="top">
    <requestFocus />
</EditText>

使用 android:singleLine="true"; - Rahul Baradia
但我想将其变成多行。 - Avinash Kumar Pankaj
@Fattie 这个问题已经有答案了。你能解释得更详细一些吗? - Linh
嗨@PhanVanLinh,没什么好解释的,将赏金添加到“奖励”和其他原因是很正常的。在另一个问题上,点击“赏金”,你会看到选项。 - Fattie
5个回答

37

要固定editText的大小,您可以使用

android:singleLine="true"

但它可以将你的editText文本框限制为1行。

如果你想要在editText中有更多行,则使用以下属性。

使用 android:lines="3"


我希望在编辑文本中输入的所有文本都可以垂直滚动。 - Avinash Kumar Pankaj
@Avinash 使用 android:lines 属性。 - Chirag

12

使用 android:lines="2" 来解决多行问题,使用android:singleLine="true" 来解决单行问题。

例如,像这样使用来展示多行

<EditText
    android:id="@+id/.."
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="@android:drawable/editbox_background_normal"
    android:ems="10"
    android:hint="Name"
    android:maxLength="20"
    android:lines="2"// fix the multiline limit 
    android:textColor="#000000" />

例如,像这样使用,如所示,用于单行

<EditText
    android:id="@+id/.."
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="@android:drawable/editbox_background_normal"
    android:ems="10"
    android:hint="Name"
    android:maxLength="20"
    android:singleLine="true"// fix the single line limit 
    android:textColor="#000000" />

5

试试这个;

android:inputType="textMultiLine"(文本多行输入)

 <EditText
        android:inputType="textMultiLine"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

4

由于singleline已经被弃用,请使用inputType:multiline,并使用lines标签设置所需的行数。

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textMultiLine"
    android:lines="4"/>

0

我有一个类似的要求,但最终决定不改变框的大小。即文本键入时会随之增长但在阻挡其他小部件之前停止。我在寻找期间阅读了这篇文章并找到了解决方案。决定将其发布回来。将大小限制为某个值可能对不同屏幕产生不同的影响。所以我认为最好让它保持原样。

关键是将编辑文本放在线性布局中与其余小部件一起。 代码和快照在这里 带有确定取消按钮的多行编辑框


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