如何使按钮的高度与另一个元素的高度相匹配?

发布于 2024-10-02 05:38:46 字数 321 浏览 2 评论 0原文

我想在 EditText 旁边放置一个按钮,并且希望它们的高度匹配。

例如,在内置 Android 浏览器中:

alt text

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:

alt text

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

没有你我更好 2024-10-09 05:38:46

大概 EditTextButton 位于 RelativeLayout 内。将按钮的布局属性设置为 EditTextalignTopalignBottom

Presumably the EditText and the Button are inside a RelativeLayout. Set the button's layout attributes to alignTop and alignBottom of the EditText.

南街女流氓 2024-10-09 05:38:46

您需要将 EditText 设为 wrap_content 的高度,并将 Button 设为 fill_parent。您需要将它们都包装在 Layout 父级中。这样它们就与相同的 Layout 父级相关联。

尝试一下,看看效果如何,如果有帮助请告诉我。如果没有,也许我可以给你一些代码来帮助你。

You will need to make the EditText wrap_content on its height and have the Button just fill_parent. You will need to have them both wrapped in a Layout parent. That way they are associated with the same Layout 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.

儭儭莪哋寶赑 2024-10-09 05:38:46

你想要的是相对布局。带有一些注释的示例如下:

我们从这个 RelativeLayout 作为父级开始。这样就可以包裹所有内容了。
在该父级中,我们放置了 2 个元素,即示例中的按钮和 editText。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

我们首先将 Button 元素放置在右上角。这就是 layout_alignParentRightlayout_alignParentTop 的全部内容。同样,这是最大的元素,因此我们将使用 wrap_content 来包裹所有内容的高度和宽度。

<Button
    android:id="@+id/Button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="some_text" />

现在,我们想要将第二个元素 editText 与前一个元素的左侧对齐,使用带有 layout_toLeftOf 参数的 id 引用来完成此操作。

<EditText
    android:id="@+id/EditText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@+id/Button1"
    android:hint="some_hint"
    android:inputType="textCapWords" />

关闭RelativeLayout,然后渲染它,看看您可能已经得到了什么。

</RelativeLayout>

在此处输入图像描述

由于 editText 的高度较小,因此与放置在其旁边的按钮不匹配。解决方案是添加更多布局参数。您正在寻找的神奇元素是 layout_alignBottomlayout_alignParentTop

   android:layout_alignBottom="@+id/Button1"
    android:layout_alignParentTop="true"

添加这两个,你就得到了正确的布局。

在此处输入图像描述

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.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

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.

<Button
    android:id="@+id/Button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="some_text" />

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.

<EditText
    android:id="@+id/EditText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@+id/Button1"
    android:hint="some_hint"
    android:inputType="textCapWords" />

Close the RelativeLayout and now render this to see what you probably got at yourself already.

</RelativeLayout>

enter image description here

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.

   android:layout_alignBottom="@+id/Button1"
    android:layout_alignParentTop="true"

Add these 2 and you get your layout right.

enter image description here

月下伊人醉 2024-10-09 05:38:46

随着 ConstraintLayout 的引入,使用 match-constraint 属性这个任务变得更加简单

With ConstraintLayout's introduction, this task has become far more simple using the match-constraint attribute

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文