首页 > 解决方案 > 如何在 Imageview 上显示 Imageview

问题描述

我需要在图像中显示 4 个如下图所示的图像视图。每个 Imageview 的宽度应为屏幕宽度的 2/5,如下图所示。我尝试使用 LinearLayout,但没有得到预期的输出。

输出应该是这样的

我的xml代码,

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

        <ImageView
            android:id="@+id/card1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="2dp"
            android:layout_marginRight="2dp"
            android:layout_weight="1"
            android:scaleType="fitCenter" />

        <ImageView
            android:id="@+id/card2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="2dp"
            android:layout_marginRight="2dp"
            android:layout_weight="1"
            android:scaleType="fitCenter" />

        <ImageView
            android:id="@+id/card3"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="2dp"
            android:layout_marginRight="2dp"
            android:layout_weight="1"
            android:scaleType="fitCenter" />

        <ImageView
            android:id="@+id/card4"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="2dp"
            android:layout_marginRight="2dp"
            android:layout_weight="1"
            android:scaleType="fitCenter" />

    </LinearLayout>

我应该修改什么?

标签: androidandroid-layout

解决方案


您需要使用weightsum意味着您需要提供三个imageviewasweight="1"并分配weight="2"给第四个图像视图。

然后,最后你需要weightsum="5"在父布局中添加你的总重量。因此,您可以获得 2/5 的屏幕宽度。

请试试这个并用下面的代码替换你的代码: -

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:weightSum="5"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/card1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="2dp"
            android:layout_marginRight="2dp"
            android:layout_weight="1"
            android:scaleType="fitCenter" />

        <ImageView
            android:id="@+id/card2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="2dp"
            android:layout_marginRight="2dp"
            android:layout_weight="1"
            android:scaleType="fitCenter" />

        <ImageView
            android:id="@+id/card3"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="2dp"
            android:layout_marginRight="2dp"
            android:layout_weight="1"
            android:scaleType="fitCenter" />

        <ImageView
            android:id="@+id/card4"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="2dp"
            android:layout_marginRight="2dp"
            android:layout_weight="2"
            android:scaleType="fitCenter" />

    </LinearLayout>

它会帮助你。


推荐阅读