Android 在方向更改时更改ImageView中的图像无效

Android 在方向更改时更改ImageView中的图像无效,android,Android,我在android上有活动:configChanges=“方向|键盘隐藏” 当设备方向发生以下变化时,我尝试在ImageView中更改图像: public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); // refresh the instructions image ImageView instructions = (ImageView

我在android上有活动:configChanges=“方向|键盘隐藏”

当设备方向发生以下变化时,我尝试在ImageView中更改图像:

public void onConfigurationChanged(Configuration newConfig) {
  super.onConfigurationChanged(newConfig);

  // refresh the instructions image
  ImageView instructions = (ImageView) findViewById(R.id.img_instructions);
  instructions.setImageResource(R.drawable.img_instructions);
}
这只在手机第一次旋转时起作用,但在旋转之后不起作用


有人能告诉我为什么会这样吗?

我想你是想做这样的事

public void onConfigurationChanged(Configuration newConfig) {
  super.onConfigurationChanged(newConfig);

  // refresh the instructions image
    ImageView instructions = (ImageView) findViewById(R.id.img_instructions);

    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
      instructions.setImageResource(R.drawable.img_instructions_land);
    } else {
      instructions.setImageResource(R.drawable.img_instructions_port);
    }
}

我想你是想做这样的事

public void onConfigurationChanged(Configuration newConfig) {
  super.onConfigurationChanged(newConfig);

  // refresh the instructions image
    ImageView instructions = (ImageView) findViewById(R.id.img_instructions);

    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
      instructions.setImageResource(R.drawable.img_instructions_land);
    } else {
      instructions.setImageResource(R.drawable.img_instructions_port);
    }
}

使用这个你可以多次改变方向,很好

public class VersionActivity extends Activity {

    ImageView img;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        img = (ImageView) findViewById(R.id.imageView1);


    }
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        // Checks the orientation of the screen
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {

            img.setImageResource(R.drawable.splash);

        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){

            img.setImageResource(R.drawable.icon);
        }
      }

}

有关更多信息,请转到

使用此功能,您可以多次更改方向,这很好

public class VersionActivity extends Activity {

    ImageView img;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        img = (ImageView) findViewById(R.id.imageView1);


    }
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        // Checks the orientation of the screen
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {

            img.setImageResource(R.drawable.splash);

        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){

            img.setImageResource(R.drawable.icon);
        }
      }

}

有关更多信息,请转到

谢谢,如果我对肖像和风景使用不同的图像名称,这两个答案都是正确的

要使用相同的名称,请执行以下操作:

public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    // refresh the instructions image
    ImageView instructions = (ImageView) findViewById(R.id.instructions);
    // prevent caching
    try {
      instructions.setImageResource(0);
    } catch (Throwable e) {
      // ignore
    }
    instructions.setImageResource(R.drawable.img_instructions);
  }

谢谢,这两个答案都是正确的,如果我使用不同的肖像和风景图片的名称

要使用相同的名称,请执行以下操作:

public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    // refresh the instructions image
    ImageView instructions = (ImageView) findViewById(R.id.instructions);
    // prevent caching
    try {
      instructions.setImageResource(0);
    } catch (Throwable e) {
      // ignore
    }
    instructions.setImageResource(R.drawable.img_instructions);
  }

Peceps自己的答案是正确的,因为它不依赖于给可提取的东西起不同的名字。似乎资源id已缓存,但可绘制的未缓存,因此我们可以将可绘制的内容提供给ImageView,而不是资源id,从而使解决方案更加优雅(避免try/catch块):


Peceps自己的答案是正确的,因为它不依赖于给可提取的东西起不同的名字。似乎资源id已缓存,但可绘制的未缓存,因此我们可以将可绘制的内容提供给ImageView,而不是资源id,从而使解决方案更加优雅(避免try/catch块):