为什么 ImageView 的 setImageResource() 方法会将图片的大小设置为 match_parent?

4
这是XML布局。

<TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="30dp"
        android:text="@string/title"
        android:textSize="@dimen/title_textSize"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@color/main_text_color"/>

<ImageView
        android:id="@+id/play_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/title"
        android:layout_centerHorizontal="true"
        android:onClick="clickPlay"
        android:src="@drawable/pause"
        />

<SeekBar
        android:id="@+id/seekbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginLeft="25dp"
        android:layout_marginRight="25dp"
        android:layout_marginTop="15dp"
        android:background="@null"
        android:paddingBottom="8dp"
        android:paddingLeft="16dp"
        android:paddingRight="10dp"
        android:paddingTop="12dp"
        android:progressDrawable="@drawable/seek_progress"
        android:thumb="@drawable/seek_thumb"/>

<ImageView
        android:id="@+id/restart_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/play_img"
        android:layout_centerHorizontal="true"
        android:onClick="clickRestart"
        android:src="@drawable/restart"
        android:visibility="invisible"
        />

这里有一个名为clickPlay()的方法。

public void clickPlay(View v) {
    if (playerCurrent != null && playerCurrent.isPlaying()) {
        playerCurrent.pause();
        playerBackground.pause();
        imgPlayPause.setImageResource(R.drawable.play);
        mVolumeSeekBar.setVisibility(View.INVISIBLE);
        imgRestart.setVisibility(View.VISIBLE);
        mIsPlaying = false;
    } else {
        playerCurrent.start();
        playerBackground.start();
        imgPlayPause.setImageResource(R.drawable.pause);
        mVolumeSeekBar.setVisibility(View.VISIBLE);
        imgRestart.setVisibility(View.INVISIBLE);
        mIsPlaying = true;
    }
}

然而,当我将图像从暂停更改为播放时,播放图像的大小变为match_parent,但当我再次按下并将图像更改为暂停图像时,它会获得普通的wrap_content大小。
播放和暂停图像完全相同!
以下是更改前后的图像(已进行颜色处理以更好地查看大小):

尝试在两种情况下使用相同的可绘制对象,以排除可绘制资源的问题。 - ci_
你的播放和暂停图标在像素大小上是一样的吗? - Lamorak
@Lamorak 是的,完全相同。 - sandalone
它们在同一个文件夹里吗?这很奇怪。。 - Lamorak
@sandalone 你尝试过暂时将相同的drawable用于两个ImageView来进行调试吗? - ci_
显示剩余3条评论
2个回答

0

这不是 Android SDK 的问题。这可能是 Android 模拟器或 Android Studio 的问题。

换句话说,当我从模拟器中卸载应用程序并执行 Build->Rebuild Project 后,该错误消失了。显然,这两个工具都没有正确地刷新 /res 文件夹中的图像。


0

我也遇到了同样的问题。在用setImageResource替换图像后,XML正常工作。关键是要有:

android:scaleType="fitXY"

在XML中。不知道为什么它可以解决问题,但它确实做到了。

我图片的完整布局如下:

            <ImageButton
            android:id="@+id/button_chart"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_weight="3"
            android:adjustViewBounds="false"
            android:background="@drawable/charts_button"
            android:contentDescription="Nautical Charts View"
            android:cropToPadding="false"
            android:scaleType="fitXY"/>

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