gpt4 book ai didi

android - 如何创建 Button 至少与 TextView 一样长的布局?

转载 作者:太空狗 更新时间:2023-10-29 16:37:44 31 4
gpt4 key购买 nike

我正在努力实现以下目标:

  • 我需要在布局中的某处放置一个Button View
  • 在用作标签的Button 正上方应该有TextView
  • Button 的大小应取决于 TextView 的大小。由于该应用已本地化,因此文本宽度不固定,而是取决于用作标签的本地化文本。
  • Button 应该有一个分钟。宽度为 150dp。如果 TextView 的宽度大于此宽度,则 Button 的宽度应与 TextView 的宽度相同。

这看起来应该是这样的:

Short Label
+-------------+
+-------------+

Very Long Label Text
+------------------+
+------------------+

我尝试了使用 LinearLayout 或 RelativeLayout 作为根布局的不同解决方案,但问题始终相同:我无法使 Button 的宽度适合 TextView< 的宽度。给 Button 一个 150dp 的最小宽度当然没有问题。但是如何解决带有长标签的第二种情况呢?

<SomeRootLayout ...>
<!-- also tried LinearLayout, but problem is the same... -->
<RelativeLayout
android:id="@+id/editTextAndLabelContainer"
...>

<TextView
android:id="@+id/labelTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A " />

<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/labelTV" />
</RelativeLayout>
</SomeRootLayout>

如果 Button 的宽度为“match_parent”,则该宽度未与 TextView 对齐,而是设置为整个屏幕宽度。

我该如何解决这个问题?

最佳答案

<RelativeLayout
android:id="@+id/editTextAndLabelContainer"
...>

<TextView
android:id="@+id/labelTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A " />

<Button
android:id="@+id/button"
android:layout_alignLeft="@+id/labelTV" //<== add this line
android:layout_alignRight="@+id/labelTV" //<== add this line
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/labelTV" />
</RelativeLayout>

关于android - 如何创建 Button 至少与 TextView 一样长的布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24504281/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com