[英]How to prevent the width of an Edittext change by the size of the text entered?
I have two EditText as follows: 我有两个EditText,如下所示:
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:weightSum="1" android:orientation="horizontal"> <EditText android:id="@+id/edtTxtNameReg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:maxLines="1" android:inputType="text" android:layout_weight="0.5" android:hint="Name"/> <EditText android:id="@+id/edtTxtSurNameReg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:maxLines="1" android:inputType="text" android:layout_weight="0.5" android:hint="Surname"/> </LinearLayout>
As I write in edtTxtNameReg, its width increases and the width of edtTxtSurNameReg decreases. 当我写edtTxtNameReg时,它的宽度增加而edtTxtSurNameReg的宽度减小。
Is there a way to keep the width of edtTxtNameReg constant and the text scrolling to the left? 有没有办法保持edtTxtNameReg的宽度不变,并且文本向左滚动?
Thanks 谢谢
Use android:layout_width="0dp"
for both EditText and the width will not vary with the content 将android:layout_width="0dp"
用于EditText,宽度不会随内容而变化
For LinearLayout when android:layout_weight
is used the width for horizontal orientation or the height for vertical must be ="0dp"
对于使用android:layout_weight
LinearLayout,水平方向的宽度或垂直方向的高度必须为="0dp"
Most of what Tonteria24 said is accurate, but not this part: Tonteria24所说的大多数内容都是准确的,但这一部分不正确:
For
LinearLayout
whenandroid:layout_weight
is used the width for horizontal orientation or the height for vertical must be="0dp"
对于使用android:layout_weight
LinearLayout
,水平方向的宽度或垂直方向的高度必须为="0dp"
The way layout_weight
in LinearLayout
works is: LinearLayout
layout_weight
的工作方式为:
It is very common to give all views in the LinearLayout
a size of 0dp
and equal weights, or to give exactly one view a size of 0dp
and weight of 1
. 在LinearLayout
为所有视图赋予大小为0dp
且权重相等的视图是很常见的,或者在一个视图中给其赋予恰好大小为0dp
且权重为1
视图是很常见的。 This makes sense because it basically skips my first point above (the child asks for 0 width or height) and then divides the space up evenly. 这是有道理的,因为它基本上跳过了我的第一点(孩子要求宽度或高度为0),然后将空间平均分配。
However, combining wrap_content
and layout_weight
is perfectly valid. 但是,将wrap_content
和layout_weight
结合使用是完全有效的。
But , it will cause different behavior. 但是 ,它将导致不同的行为。 First the LinearLayout
will give the first EditText
enough width to wrap its content, then it will (try to) give the second EditText
enough width to wrap its content, and then it will divide any extra space between the two views. 首先, LinearLayout
将给第一个EditText
足够的宽度来包装其内容,然后(尝试)给第二个EditText
足够的宽度来包装其内容,然后它将在两个视图之间划分任何多余的空间。
The result? 结果? As the width of the first EditText
's contents grows, the "extra" space left over to divide shrinks. 随着第一个EditText
内容的宽度增加,用于分割的“多余”空间会缩小。 The first EditText
will eventually grow to fill the entire LinearLayout
if you enter enough text. 如果您输入足够的文本,第一个EditText
最终将增长为填充整个LinearLayout
。
So that's why setting its width to 0dp solves it for you. 因此,将其宽度设置为0dp即可为您解决。 It stops the growing behavior. 它阻止了行为的增长。 But there may be another time when you want to use wrap_content
and layout_weight
together, and that's perfectly fine. 但是,可能还有另一wrap_content
同时使用wrap_content
和layout_weight
,这很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.