android - child 视线高度相等

标签 android android-layout android-linearlayout android-xml

我有一个水平LinearLayout,其中有两个按钮,里面有文本。 LinearLayout 和按钮 layout_heightwrap_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": without android:baselineAligned="false"

这里有: with 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>

看起来像这样: enter image description here

关于android - child 视线高度相等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36871276/

相关文章:

android - 根据 Android 应用程序版本使用特定的应用程序引擎版本

android - 用于相等间距和性能的嵌套布局权重

图片上方的 Android ScrollView

安卓:未知主机异常

android - Android:AWS Amplify用户状态未获取更新

android - 出现软键盘时,工具栏上移

Android应用程序布局背景通过布局中的控件显示

Android - 制作一个包含可滚动 ListView 的 LinearLayout

java - Android:在Java/Kotlin中的ScrollView内部居中LinearLayout

android - Android Marshmallow 的 ParseApacheHttpClient 问题 : can't find referenced method SSLSocketFactory. getHttpSocketFactory