【问题标题】:Why does ImageView setImageResource() sets image size to match_parent?为什么 ImageView setImageResource() 将图像大小设置为 match_parent?
【发布时间】:2015-03-19 17:28:04
【问题描述】:

这是 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

播放和暂停图像是相同的!

这是它之前和之后的样子(为它们着色,以便您更好地查看尺寸)

【问题讨论】:

  • 尝试在两种情况下使用相同的drawable来排除drawable资源的问题。
  • 您是否播放和暂停相同大小的可绘制对象(以像素为单位)?
  • @Lamorak 是的,完全相同
  • 它们在同一个文件夹中吗?这很奇怪..
  • @Lamorak 当然,两者都在 /drawable-large 中(7in 平板电脑资源)。是的,确实很奇怪。在我开始在平板电脑上进行测试之前,我实际上并没有在移动设备上注意到这一点。

标签: android android-layout android-imageview


【解决方案1】:

这不是 Android SDK 的问题。这是 Android Emulator 或 Android Studio 的问题。

也就是说,当我从模拟器中卸载应用程序并执行Build-&gt;Rebuild Project 时,错误就消失了。显然这两个没有正确刷新/res文件夹中的图像。

【讨论】:

    【解决方案2】:

    我有同样的问题。 XML 工作正常,直到我用调用 setImageResource 替换了图像。诀窍是:

    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"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多