如何将一个布局组件的高度设置为另一个组件相同的高度?

9

如何将一个布局组件的高度设置为另一个组件的相同高度:

例如:

我可以设置android:layout_height="@+id/info_box.height"或类似的操作吗?

我想让ImageView的高度与我的LinearLayout相匹配。

    <ImageView android:id="@+id/border"
    android:src="@drawable/frame"
    android:layout_below="@+id/title"
    android:layout_width="fill_parent"
    android:layout_height="????"    
    android:scaleType="fitXY" 
    android:drawingCacheQuality="auto"
    />

    <LinearLayout
    android:id="@+id/info_box"
    android:orientation="vertical"
    android:layout_below="@+id/title"
    android:background="@layout/my_bg"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
    ... other stuff . .
     </LinearLayout>

嗯,为什么不把你的ImageView放在LinearLayout里面,并将高度设置为fill_parent呢? - Falmarri
@Falmarri:如果他的ImageView和LinearLayout在一个水平LinearLayout中(未指定),那么结果可能不同。此外,子元素可以使用fill_parent,父元素可以使用wrap_content吗?这似乎几乎是自相矛盾的。 - Kevin Coppock
需要更多信息。您的ImageView和LinerLayout所在的根ViewGroup是什么?您想通过布局实现什么目标? - Octavian Helm
这是一个简单的问题,我能否将一个组件的高度设置为另一个组件的高度,即使它们不在同一个容器中。我不需要重新布置组件的替代方法。 - steve
那么,特定的答案是否是否定的。没有直接的方法来实现你所建议的方式。如果你愿意尝试其他方法来达到类似的结果,请发布更多信息。 - Kevin Coppock
我知道的唯一方法是使用 RelativeLayout 和它的相对对齐方式。 - bigstones
3个回答

5
唯一的实现方法是将两个视图放在同一个容器中,而LinearLayout或RelativeLayout通常很适合这种情况。你也可以通过代码实现。

如果它们是同一个容器的一部分,你会怎么做?我认为他或她想要将ImageView缩放到LinearLayout的高度。 - user153275
算了,我找到了。在ImageView上:scaletype="centercrop",layout_height="fill_parent",和layout_width="fill_parent"。关键是,scaletype="centercrop"在布局编辑器上不起作用。 - user153275

1

您可以使用android:layout_weight

将您的两个视图(border和info_box)与fill_parent和相同值的layout_weight放置在具有orientation=vertical的同一LinearLayout中。

使用此技术,border和info_box将具有相同的高度。

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/border"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/title"
        android:layout_weight="2"
        android:drawingCacheQuality="auto"
        android:scaleType="fitXY"
        android:src="@drawable/frame" />

    <LinearLayout
        android:id="@+id/info_box"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/title"
        android:layout_weight="2"
        android:background="@layout/my_bg"
        android:orientation="vertical" >

        ... other stuff . .

    </LinearLayout>

</LinearLayout>

1
他不想所有的视图都使用fill_parent。他希望其中一个是wrap_content,另一个与第一个一样高。 - Danail
他可以更改第一个LinearLayout的宽度和高度,并选择所需的大小。但是ImageView和第二个LinearLayout的高度(或宽度)将始终相同,因为它们具有相同的layout_weight。 - fingerup

0
在RelativeLayout中,只需将以下行添加到“具有相同高度的元素”中即可:
android:layout_alignBottom="@+id/element_with_needed_height"
android:layout_alignTop="@+id/element_with_needed_height"

当然,在这种情况下,元素将处于同一级别


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接