【问题标题】:strange behavior of image view on changing the orientation of the screen更改屏幕方向时图像视图的奇怪行为
【发布时间】:2011-01-12 10:02:35
【问题描述】:

我正在使用 imageView 来显示图像。稍后我使用imageView.setImage(bitmap)为视图设置不同的图像。现在如果我将屏幕的方向从纵向更改为横向,imageview 将显示旧图像而不是我设置的新图像。有人可以告诉我为什么会发生这种情况以及如何克服这个问题。

【问题讨论】:

    标签: android imageview screen-orientation android-configchanges


    【解决方案1】:

    正常情况下,当您更改方向时,活动将重新创建(调用 onCreate())。
    如果两个方向只有一个 xml,则可以通过
    阻止它 1.在manifest文件中设置activity为android:configChanges="orientation"

    <activity android:name=".Youractivityname" 
          android:configChanges="orientation"/>
    

    2。然后在我们的活动类中覆盖它

    public class Youractivityname extends Activity {
    
         public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
                // all your codes 
          }
    
        @Override
            public void onConfigurationChanged(Configuration newConfig) {
              super.onConfigurationChanged(newConfig);
    
            }
    }
    

    希望你现在很清楚

    【讨论】:

    【解决方案2】:

    默认情况下,在方向更改时,活动被销毁并重新创建,因此如果您在创建时设置第一个位图,然后在其他位置更改为第二个位图,当方向更改时,第一个图像在 onCreate 中设置但如果代码不会再次调用更改图像的图像,然后图像不会更改。

    【讨论】:

    • 好的。这解释了这种行为。有什么办法可以克服。
    【解决方案3】:

    在清单中使用android:configChanges="orientation" 是一种不好的做法。 This 是处理方向变化的推荐方式。

    【讨论】:

      【解决方案4】:

      我在将屏幕纵向更改为横向图像隐藏时遇到问题。我在下面的活动中使用了 AndroidManifest.xml 中的行,我的问题已解决

       android:configChanges="orientation|screenSize"
      

      【讨论】:

        猜你喜欢
        • 2012-05-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-22
        • 2013-03-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多