如何防止EditText在用户输入时调整其自身大小?

问题描述:

我有一个EditTextButton在同一水平线上彼此相邻设置。它看起来很棒,除非用户输入大量文字,EditText被调整大小,并且Button被压扁。如何防止EditText在用户输入时调整其自身大小?

我有EditTextButton设置为layout_width="wrap_content""fill_parent"弄乱了布局,如果我不需要,我不想使用绝对大小 - 现在它在横向和纵向上看起来都很棒,我只是不想让EditText调整大小。

我的布局:

<TableLayout 
    android:id="@+id/homelayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <TableRow> 
     <TextView 
      android:id="@+id/labelartist" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="Find artists:" /> 
    </TableRow> 
    <TableRow> 
     <EditText 
      android:id="@+id/entryartist" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="6" 
      android:background="@android:drawable/editbox_background" 
      android:editable="true" 
      android:padding="5px" 
      android:singleLine="true" /> 
     <Button 
      android:id="@+id/okartist" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_marginLeft="10dip" 
      android:layout_weight="1" 
      android:text="Search" /> 
    </TableRow> 

</TableLayout> 

对于EditText上使用

android:layout_width="0dp" - not required but preferred. 
android:layout_weight="1" 

而对于巴顿不指定android:layout_weight

+0

谢谢 - 这个伎俩! “0dp”代表宽度是什么意思?它应该占用所有可用空间? – 2009-09-11 15:36:59

+1

是的,它会的。 layout_weight标签将做到这一点。 – bhatt4982 2009-09-11 16:26:34

+1

不要忘记在你的'TableLayout'中删除(如果存在)'android:stretchColumns =“x”',其中“x”是包含你不想自动调整大小的'EditText'的列位置。 – Caumons 2012-03-16 12:55:53

EditText一个maxWidth。这应该阻止它调整超出你提供的宽度。另外,如果您希望将其包含在1行内,则maxLines设置也会设置为1。

+3

谢谢 - 我真的不想硬编码特定maxWidth,因为它不会对不同的方向或屏幕尺寸相同。 – 2009-09-11 15:37:55

+1

对'maxLines'上的提示+1,它确实有诀窍。 – 2012-07-01 11:07:41

我也有类似的问题,但我不能得到上述工作。我所做的是从EditText框中获取文本,然后将其格式化为我的屏幕可以处理的最大尺寸。当我把它保存到数据库中时,我将它保存为显示文本和原始文本。

如果你去to this page我把更详细的解释。

只需使用:

android:layout_width="fill_parent" 

两者的EditText和Button。它也可以为EditText设置,但最好在两者上都有。

正确的方式做,这是设置了android:minWidth和android:maxWidth等于你想要的宽度。这样你就不必调整你的布局来使用android:layout_weight。

例如,如果你希望你的EditText永远是80个密度像素,无论你使用多少个字符键入以下内容:

android:minWidth="80dp" 
android:maxWidth="80dp" 

我要做的就是在OnCreate的活动,测量的EditText首先应用其最大宽度。

你可以这样做,使用类似于下面的代码:

EditText someedittext = (EditText)findViewById(R.id.sometextview); 
someedittext.setMaxWidth(someedittext.getWidth()); 
+1

更好的解决方案! – 2014-03-25 10:43:40

+0

非常感谢...没有soln为我工作,但这一个。 – kgandroid 2014-09-05 06:03:43

因为它显示了上述

android:minWidth="80dp" 
android:maxWidth="80dp" 

android:layout_width="60dp" 

你可以定义你的EditText的大小

你可以应用一个你想要的尺寸的背景图片 但是不要忘记用wrap_content来定义它的高度和宽度 。

android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 

我有确切的查询。但我有一个EditText和一个按钮在一个线性布局中彼此相邻。 我试过android:layout_weight =“1”为EditText android:layout_width =“0dp” 它的工作原理非常完美! 非常感谢!

enter image description here

您需要添加机器人:imeOptions防止这种行为。

Click here