Android 当用户键入时,如何防止 EditText 调整自身大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1408781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I prevent an EditText from resizing itself when the user is typing?
提问by alex_c
I have an EditText
and a Button
set next to each other on the same horizontal line. It looks great, except when the user enters a lot of text, the EditText
is resized, and the Button
is squished.
我有一个EditText
和一Button
组在同一条水平线上彼此相邻。它看起来很棒,除了当用户输入大量文本时,EditText
调整大小并Button
压扁。
I have both EditText
and Button
set to layout_width="wrap_content"
. "fill_parent"
messes up the layout, and I don't want to use absolute sizes if I don't have to - the way it is now looks great in both landscape and portrait, I just don't want the EditText
to resize.
我有两个EditText
并Button
设置为layout_width="wrap_content"
. "fill_parent"
弄乱了布局,如果没有必要,我不想使用绝对尺寸 - 现在它在横向和纵向看起来都很棒,我只是不想EditText
调整大小。
My layout:
我的布局:
<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>
回答by bhatt4982
For EditText use
对于 EditText 使用
android:layout_width="0dp" - not required but preferred.
android:layout_weight="1"
And for Button dont specify android:layout_weight
对于 Button 不指定 android:layout_weight
回答by Kevin Parker
What I do is in the onCreate for the activity, measure the EditText first then apply its maxWidth.
我所做的是在活动的 onCreate 中,首先测量 EditText,然后应用其 maxWidth。
You can do so using code similar to the following:
您可以使用类似于以下内容的代码执行此操作:
EditText someedittext = (EditText)findViewById(R.id.sometextview);
someedittext.setMaxWidth(someedittext.getWidth());
回答by Prashast
Give EditText
a maxWidth
. That should stop it from resizing beyond the width you provided. Also, if you want it to be contained within 1 line set the maxLines
setting to 1 too.
给EditText
一个maxWidth
。这应该会阻止它调整大小超出您提供的宽度。此外,如果您希望将其包含在 1 行内,请将maxLines
设置也设置为 1。
回答by Dhina k
You need to add android:imeOptions to prevent this behavior.
您需要添加 android:imeOptions 来防止这种行为。
回答by Kamen
Just use:
只需使用:
android:layout_width="fill_parent"
for both EditText and Button. It will also work set for EditText but it is better to have it on both.
对于 EditText 和 Button。它也适用于 EditText,但最好同时使用它。
回答by Android developer
you can define size of your edittext as it shows above
您可以定义编辑文本的大小,如上所示
android:minWidth="80dp"
android:maxWidth="80dp"
or
或者
android:layout_width="60dp"
or
或者
you can apply a background image of size you want but don't forget to define its height and width with wrap_content.
您可以应用所需大小的背景图像,但不要忘记使用 wrap_content 定义其高度和宽度。
android:layout_width="wrap_content"
android:layout_height="wrap_content"
回答by Shruti Dasgopal
I had the exact query. But i had an EditText and a Button next to each other in a Linear Layout. I tried android:layout_weight="1" for EditTextandroid:layout_width="0dp" And it worked just perfect!! Thanks a lot!
我有确切的查询。但是我在线性布局中有一个 EditText 和一个 Button 彼此相邻。我试过android:layout_weight="1" for EditTextandroid:layout_width="0dp" 它工作得非常完美!!非常感谢!
回答by zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
The correct way to do this would be to set the android:minWidth and android:maxWidth equal to the width you want. This way you will not have to adjust your layout to use the android:layout_weight.
正确的方法是将 android:minWidth 和 android:maxWidth 设置为您想要的宽度。这样您就不必调整布局来使用 android:layout_weight。
For example if you want your EditText to always be 80 density pixels no matter how many characters you type in use the following:
例如,如果您希望 EditText 始终为 80 密度像素,无论您输入多少个字符,请使用以下内容:
android:minWidth="80dp"
android:maxWidth="80dp"
回答by zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
I had a similar problem, but I couldn't get the above to work. What I did, is take in the text from EditText box and then format it down to the max size that my screen could handle. When I save this off to the DB, I will save it as display Text and original text.
我有一个类似的问题,但我无法使上述工作正常工作。我所做的是从 EditText 框中获取文本,然后将其格式化为我的屏幕可以处理的最大尺寸。当我将其保存到数据库时,我会将其保存为显示文本和原始文本。
If you go to this pageI put a longer explanation.
如果你去这个页面,我会给出更长的解释。
回答by josedacruz
The chosen solution works, but let me add a little complement:
if you use
android:layout_weight="0.15"
(the value is not important)
thenandroid:layout_width="0dp"
if the LinearLayout
is Horizontal
orandroid:layout_height="0dp"
if the LinearLayout
is Vertical.
选择的解决方案作品,但让我补充一点补充:
如果你使用
android:layout_weight="0.15"
(值并不重要),
然后android:layout_width="0dp"
如果LinearLayout
是水平
或android:layout_height="0dp"
如果LinearLayout
是垂直的。