android - 如何在 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>
我应该修改什么?
解决方案
您需要使用weightsum
意味着您需要提供三个imageview
asweight="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>
它会帮助你。
推荐阅读
- java - Make recursive API call using Retrofit
- javascript - Reactjs 变量在 useEffect 之后返回未定义
- python - 如何显示图像并在 1.5 秒后将其杀死?
- vue.js - Vue 3:将 Options API 组件转换为 Composition API(将容器设置为元素 - this.$el vs ref?)
- blockchain - 为什么我的以太坊智能合约交易在 X 笔交易后没有显示?
- c++ - 这个递归案例有什么问题?
- vapor-fluent - 如何使用 Vapor 添加约束?
- python - Python 调度器说明
- git - 接受主分支名称和主分支名称的 git 别名?
- javascript - Javascript竞争条件多个承诺连接字符串