[英]How do I prevent an EditText from resizing itself when the user is typing?
I have an EditText
and a Button
set next to each other on the same horizontal line.我有一个EditText
和一个Button
设置在同一条水平线上彼此相邻。 It looks great, except when the user enters a lot of text, the EditText
is resized, and the Button
is squished.它看起来很棒,除了当用户输入大量文本时, EditText
被调整大小,并且Button
被压扁。
I have both EditText
and Button
set to layout_width="wrap_content"
.我将EditText
和Button
设置为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. "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>
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
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());
Give EditText
a maxWidth
.给EditText
一个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.此外,如果您希望它包含在 1 行内,请将maxLines
设置也设置为 1。
Just use:只需使用:
android:layout_width="fill_parent"
for both EditText and Button.对于 EditText 和 Button。 It will also work set for EditText but it is better to have it on both.它也适用于 EditText,但最好同时使用它。
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"
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.我所做的是从 EditText 框中获取文本,然后将其格式化为我的屏幕可以处理的最大尺寸。 When I save this off to the DB, I will save it as display Text and original text.当我将其保存到数据库时,我会将其保存为显示文本和原始文本。
If you go to this page I put a longer explanation.如果你去这个页面,我会给出更长的解释。
I had the exact query.我有确切的查询。 But i had an EditText and a Button next to each other in a Linear Layout.但是我在线性布局中有一个 EditText 和一个 Button 彼此相邻。 I tried android:layout_weight="1" for EditText android:layout_width="0dp" And it worked just perfect!!我试过android:layout_weight="1" for EditText android:layout_width="0dp" 它工作得非常完美!! Thanks a lot!非常感谢!
The chosen solution works, but let me add a little complement:选择的解决方案有效,但让我补充一点:
if you use android:layout_weight="0.15"
(the value is not important)如果您使用android:layout_weight="0.15"
(该值不重要)
then然后android:layout_width="0dp"
if the LinearLayout
is Horizontal android:layout_width="0dp"
如果LinearLayout
是 Horizontal
or要么android:layout_height="0dp"
if the LinearLayout
is Vertical. android:layout_height="0dp"
如果LinearLayout
是 Vertical。
The correct way to do this would be to set the android:minWidth and android:maxWidth equal to the width you want.正确的方法是将 android:minWidth 和 android:maxWidth 设置为您想要的宽度。 This way you will not have to adjust your layout to use the android:layout_weight.这样您就不必调整布局来使用 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"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.