2016-04-26 34 views
0

我有一个水平的LinearLayout与两个按钮与文本里面。 LinearLayout和按钮layout_heightwrap_content儿童的意见有相等的高度

两个按钮之一的文本需要两行,不同于其他行需要一行。

所以最后一个按钮的高度比另一个更大,这是我不想要的。

我知道如何以编程方式解决这个问题。所以我提出这个问题的原因是要知道我是否可以通过xml解决这个问题。

一个可行的办法是,对于其文字是一行按钮,设置

layout_height="match_parent" 

,它工作正常。

但这里的问题是,我通常不知道哪个按钮将占据最大的高度。

实质上我想要做的是:在内部有一个带有视图的LinearLayout 我希望将所有子视图的高度设置为等于视图的高度,当视图的内容被包装时具有最大高度。

而问题是如果这是可能的通过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> 
+0

为了谁提议的编辑之一:抱歉,它自动拒绝,因为我做了一个后来编辑。 如果您可以再次发布,我会接受它。 – Abomin

+0

请添加您的布局xml – USKMobility

+0

尽管问题更一般,我会添加一个示例。 – Abomin

回答

1

我猜你搜索什么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

+0

因此,我们可以在父级上使用wrap_content,在子级上使用match_parent? 奇怪但很好。谢谢。 – Abomin

+0

是的,从孩子的角度来看,它会匹配父母的大小 – rekire

0

用这个东西来设置权衡总和,重量布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:weightSum="100" > 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="10" 
    android:orientation="horizontal" > 

    <Button 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:text="Button 1" /> 

    <Button 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:text="Button 2" /> 
    </LinearLayout> 

    </LinearLayout>