我有一个水平LinearLayout
,其中有两个按钮,里面有文本。
LinearLayout
和按钮 layout_height
是 wrap_content
。
两个按钮之一的文本占用两行,而另一个按钮的文本占用一行。
所以最后一个按钮的高度比另一个按钮的高度大,这是我不想要的。
我知道如何以编程方式解决这个问题。所以我提出这个问题的原因是想知道是否可以通过xml来解决这个问题。
一种可能的解决方案是,对于文本为一行的按钮,设置
layout_height="match_parent"
而且效果很好。
但这里的问题是,通常我不知道哪个按钮会占据最大的高度。
本质上我想做的是:拥有一个带有 View 的 LinearLayout 我想将所有 subview 的高度设置为等于其内容包装时具有最大高度的 View 的高度。
问题是这是否可以通过 xml 实现?
XML 示例。另外我忘了补充一点,我的宽度已经是 0dp 了。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/some_string" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/some_other_string" />
</LinearLayout>
最佳答案
我猜您搜索的是android:baselineAligned="false"
,这将避免文本位于同一基线上,并且按钮将从屏幕上相同的 y 位置开始。
此处没有 android:baselineAligned="false"
:
但是,如果您还希望两个按钮大小相等,请尝试以下布局示例:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:baselineAligned="false">
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="aaaaa"/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="bbbbbbb bbbbbbbb bbbbbbbb bbbbbbbbbbbb bbbbbb"/>
</LinearLayout>
关于android - child 视线高度相等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36871276/