首页 > 解决方案 > 在 ImageView 中居中图像的问题

问题描述

我有一张图片(兔子 86*148 像素)。我有这个代码:我没用过 android:layout_gravity="center"android:gravity="center"但图像在 ImageView 的中心,为什么?

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_margin="15dp">
        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="190dp"
            android:layout_height="190dp"
            android:src="@drawable/rabbit" />
    </LinearLayout>

现在,我有这个代码。

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_margin="15dp">
        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/rabbit" />
    </LinearLayout>

图像在左侧。iv.SetScaleType(ImageView.ScaleType.CenterInside)没有效果。

标签: androidimageview

解决方案


将此添加到您的布局中

android:scaleType="fitCenter"

推荐阅读