如何使按钮的高度与另一个元素的高度相匹配?
我想在 EditText 旁边放置一个按钮,并且希望它们的高度匹配。
例如,在内置 Android 浏览器中:
Go 按钮与 EditText 字段的高度相同。我知道我可以将这两个视图包装在父布局视图中,并将它们的高度都设置为 fill_parent,这将使它们匹配。但是,我想这样做而不必为布局指定静态大小。我宁愿让 EditText 根据字体大小采用所需的任何高度,然后让其旁边的按钮匹配可能的任何高度。
这可以通过 xml 布局实现吗?
I want to put a button next to a EditText and I want their heights to match.
For example, from the built in Android browser:
The Go button is the same height as the EditText field. I know I could wrap both these views in a parent layout view, and set both of their heights to fill_parent, and that would make them match. However, I would like to do this without having to give the layout a static size. I would rather have the EditText take whatever height it needs based on the font size and then have the button next to it match whatever height that might be.
Is this possible with an xml layout?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
大概
EditText
和Button
位于RelativeLayout
内。将按钮的布局属性设置为EditText
的alignTop
和alignBottom
。Presumably the
EditText
and theButton
are inside aRelativeLayout
. Set the button's layout attributes toalignTop
andalignBottom
of theEditText
.您需要将
EditText
设为wrap_content
的高度,并将Button
设为fill_parent
。您需要将它们都包装在Layout
父级中。这样它们就与相同的Layout
父级相关联。尝试一下,看看效果如何,如果有帮助请告诉我。如果没有,也许我可以给你一些代码来帮助你。
You will need to make the
EditText
wrap_content
on its height and have theButton
justfill_parent
. You will need to have them both wrapped in aLayout
parent. That way they are associated with the sameLayout
parent.Try that and see how that works, if it helps let me know. If it doesn't maybe i can give you some code that will help you out.
你想要的是相对布局。带有一些注释的示例如下:
我们从这个 RelativeLayout 作为父级开始。这样就可以包裹所有内容了。
在该父级中,我们放置了 2 个元素,即示例中的按钮和 editText。
我们首先将 Button 元素放置在右上角。这就是 layout_alignParentRight 和 layout_alignParentTop 的全部内容。同样,这是最大的元素,因此我们将使用 wrap_content 来包裹所有内容的高度和宽度。
现在,我们想要将第二个元素 editText 与前一个元素的左侧对齐,使用带有 layout_toLeftOf 参数的 id 引用来完成此操作。
关闭RelativeLayout,然后渲染它,看看您可能已经得到了什么。
由于 editText 的高度较小,因此与放置在其旁边的按钮不匹配。解决方案是添加更多布局参数。您正在寻找的神奇元素是 layout_alignBottom 和 layout_alignParentTop。
添加这两个,你就得到了正确的布局。
What you want is a relative layout. An example with some comments is as follows
We start with this RelativeLayout as a parent. That can wrap all content.
In that parent we put 2 elements, the button and the editText from your example.
We start by placing the Button element on the top right corner. That is what the the layout_alignParentRight and layout_alignParentTop are all about. Again this is the biggest element so we will let it wrap all content using wrap_content for both height and width.
Now the second element, the editText we want to align to the left side of our previous element, use the id reference with the layout_toLeftOf parameter to accomplish just that.
Close the RelativeLayout and now render this to see what you probably got at yourself already.
Since the editText is smaller in height it won't match the Button it's placed next to. Solution for that is to add some more layout parameters. The magic ones you're looking for are layout_alignBottom and layout_alignParentTop.
Add these 2 and you get your layout right.
随着 ConstraintLayout 的引入,使用 match-constraint 属性这个任务变得更加简单
With ConstraintLayout's introduction, this task has become far more simple using the match-constraint attribute