Android EditText 和 Button 在同一行
我需要在搜索按钮
旁边有一个Edittext
。 EditText
应填充尽可能多的宽度,按钮应位于右侧,并且足够大以容纳其文本。
现在看起来像这样:
[EDIT TEXT][Search]
但应该看起来像这样:
[.......EDIT TEXT.......][Search]
这是 XML
:
<RelativeLayout android:id="@+id/RelativeLayout01" android:layout_width="fill_parent" android:layout_height="wrap_content">
<EditText android:id="@+id/search_box"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="type to filter"
android:inputType="text"
android:maxLines="1" />
<Button android:id="@+id/search_text"
android:layout_toRightOf="@id/search_box"
android:text="Search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
I need to have a Edittext
beside a Search button
. The EditText
should fill as much of the width as possible, the button should be to the right and be just big enough to have it's text.
It looks like this now:
[EDIT TEXT][Search]
but should look like this:
[.......EDIT TEXT.......][Search]
Here is the XML
:
<RelativeLayout android:id="@+id/RelativeLayout01" android:layout_width="fill_parent" android:layout_height="wrap_content">
<EditText android:id="@+id/search_box"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="type to filter"
android:inputType="text"
android:maxLines="1" />
<Button android:id="@+id/search_text"
android:layout_toRightOf="@id/search_box"
android:text="Search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
是否必须是相对布局?
我建议如下:将
EditText
layout_width
设置为fill_parent
并将其layout_weight
设置为 1,如下所示:Does it have to be Relative Layout?
I would suggest the following: set
EditText
layout_width
tofill_parent
and itslayout_weight
to 1, something like this:在这种情况下,
EditText
的宽度必须设置为fill_parent
(而不是wrap_content
):编辑
正如您所说,它隐藏了按钮。然后,只需颠倒顺序即可:
In that case, the width for the
EditText
must be set tofill_parent
(instead ofwrap_content
):Edit
As you said, it hides the button. Then, just invert the order:
我相信 Cristian 的答案适用于更高版本的 SDK,但对于之前的版本,您需要做的是首先定义 Button,然后将 EditText 放置到layout_toLeftOf="@id/search_box",顺便说一句,这两者对象应该有不同的 ID。
I believe Cristian's answer works on later versions of the SDK, but for prior versions what you'll need to do is define the Button first, and place the EditText to layout_toLeftOf="@id/search_box", which, by the way, both objects should have different IDs.
试试这个我已经修复了一点。它显示编辑文本右侧的按钮,并且还允许增加编辑文本宽度。
try this i have fixed little. it shows button right of edit text and also edit text width allowed to increase.
对于那些想要相对布局解决方案的人,请尝试这个。我的目标是保持按钮大小相同,但当手机旋转到横向模式或在不同的手机屏幕尺寸上时调整 EditText 字段。
因此,技巧是使用边距的组合并将父级与开头对齐。您可以尝试以下方法,我直接从我的应用程序中提取了它。
您将得到的是 EditText 的大小将根据屏幕宽度而变化,而按钮将在最后保持固定。由于
android:layout_alignParentEnd="true"
,按钮停留在末尾。为了阻止 EditText 超出按钮,使用了android:layout_marginEnd="100dp"
。希望这有帮助!For those who want a solution for a Relative Layout, try this. My goals was to keep the button size the same but adjust the EditText field when the phone was rotated to landscape mode or on different phone screen sizes.
So the trick is to use a combination of margins and aligning the parent to the start. Here is something you can try, I pulled this directly from my app.
What you'll get is the EditText will change in size depending on screen-width while the button will remain fixed at the end. The button sticks at the end because of
android:layout_alignParentEnd="true"
. And to stop the EditText from overrunning the Button,android:layout_marginEnd="100dp"
is used. Hope this helps!现在更好的答案是将EditText的layout_width设置为0dp
The better answer now is set layout_width of EditText to 0dp