Android XML 中有没有办法将一排按钮的宽度设置为最宽按钮?

发布于 2024-10-12 04:27:43 字数 234 浏览 6 评论 0原文

有没有办法在XML中声明一行按钮,使所有按钮具有相同的宽度,等于最宽按钮的wrap_content宽度?我熟悉将所有宽度设置为 0 并将它们的权重全部分配为 1 的技巧,但如果父布局宽度设置为wrap_content,则这将不起作用。我不想将父级宽度设置为 fill_parent,因为我不希望按钮拉伸得超过必要的程度。

我能想到的唯一方法是在代码中(或者使用每个按钮中与其他按钮通信的 onMeasure 逻辑,或者使用自定义布局类)。

Is there a way to declare a row of buttons in XML so that all the buttons have the same width, which is equal to the wrap_content width of the widest button? I'm familiar with the trick of setting all the widths to 0 and assign them all a weight of 1, but that won't work if the parent layout width is set to wrap_content. I don't want to set the parent width to fill_parent because I don't want the buttons stretched more than necessary.

The only way I can think of doing this is in code (either with onMeasure logic in each button that communicates with the other buttons or with a custom layout class).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

云归处 2024-10-19 04:27:43

我认为你必须在代码中做到这一点。

创建自定义布局类将是一种可行的方法。重写 onMeasure() 并使其看起来像这样:

  1. 对所有子项调用 setLayoutParams 以将其layout_widths 设置为 WRAP_CONTENT。
  2. 调用 super.onMeasure()
  3. 迭代子视图以找到具有最大 getMeasuredWidth() 的视图。
  4. 使用最宽的像素宽度迭代调用 setLayoutParams() 的所有其他子视图。
  5. 再次调用 super.onMeasure()。 :)

应该有效,但我不会把我的声誉押在上面...如果不行的话,我很乐意为您提供进一步的帮助。

I think you'd have to do this in code.

Creating a custom layout class would be the way to go. Override onMeasure() and make it look something like this:

  1. Call setLayoutParams on all children to set their layout_widths to WRAP_CONTENT.
  2. Call super.onMeasure()
  3. Iterate child views to find the one with the biggest getMeasuredWidth().
  4. Iterate all other child views calling setLayoutParams() with the widest pixel width.
  5. Call super.onMeasure() again. :)

That should work but I won't stake my reputation on it... happy to help you further if it doesn't.

与往事干杯 2024-10-19 04:27:43

要让按钮在 XML 中排成一行,您需要在 LinearLayout 中添加按钮并将方向更改为水平,

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
</LinearLayout>

即至于获得最宽的按钮并更改所有其他按钮以匹配,我不确定您是否会这样做您的活动中必须有某种方法来获取最宽的按钮,然后以编程方式将所有其他按钮设置为相同。

To get the buttons in a row in the XML you need to add the buttons with in a LinearLayout and change the orietnation to horizontal i.e.

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
</LinearLayout>

As for getting the widest button and changing all the other buttons to match I am not sure as a guess you would have to have some sort of method within your activity to get the widest button and then programaticaly set all the other buttons to be the same.

大姐,你呐 2024-10-19 04:27:43

只需找出最宽的按钮是什么,但它在水平宽度相匹配的视图中,然后在 << 中使用layout_width =“match_parent”或“fill_parent” 2.3.

这将使它们全部使用指定的宽度。

如果您想以编程方式执行此操作,则需要迭代所有部分,找到最大值,然后再次迭代并设置它。

Just find out what your widest button is, but it in a view with a horizontal width to match, and then use layout_width="match_parent" or "fill_parent" in < 2.3.

It'll make them all use the width assigned.

If you want to do it programatically, you need to iterate over all the sections, find the max, than iterate again and set it.

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