ImageView的图片与LinearLayout的高度不适配。

3

第一次提问,请温柔些。 :)

我正在尝试在LinearLayout中创建嵌套的ImageView布局。父级LinearLayout需要与根视图的宽度匹配,但高度由它所持有的2个ImageView组件决定。

这2个ImageView组件需要在LinearLayout父级内等分宽度(即50%),并保持其比例高度。

然而,虽然ImageView的宽度计算正确且图像本身的高度正确,但父级LinearLayout的高度不正确。

以下是我正在使用的XML代码,很抱歉,但我目前没有足够的声望点数来发布图片,这可能会让事情变得困难。

XML 代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/grey"
    android:orientation="vertical"
    android:padding="10dp" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:background="@drawable/container_dropshadow"
        android:orientation="horizontal"
        android:padding="5dp"
        android:weightSum="2" >

        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="horizontal"
            android:padding="5dp"
            android:src="@drawable/barcode" />

        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="horizontal"
            android:padding="5dp"
            android:src="@drawable/barcode" />
    </LinearLayout>

</LinearLayout>

这是我得到的东西

在此输入图片描述

非常感谢您的帮助,如果有任何失礼之处,请见谅。

Danny

1个回答

1

对于每个ImageView,您应该添加以下行:

android:adjustViewBounds="true"

如需了解有关xml属性的更多信息,请参见此处


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