【问题标题】:How to use onConfigurationChanged() and newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE in android 2.3.3如何在 android 2.3.3 中使用 onConfigurationChanged() 和 newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE
【发布时间】:2012-03-22 22:10:36
【问题描述】:

我正在使用onConfigurationChanged()。在那里,当我从 LandScape 更改为 Portrait 时,它正在调用 if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) 并从 LandScape 转换为 Portrait。但是当我从肖像更改为风景时,它不会更改为风景,因为它正在调用if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE),所以它不会从风景更改为肖像。 请帮忙。

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

        //int orientation = this.getResources().getConfiguration().orientation;

        if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
            Log.d("Entered to change as Portrait ","PPPPPPPPPPPPPPPPP");
            setContentView(R.layout.settings);
        } else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            Log.d("Entered to change as LandScape ","LLLLLLLLLLLLLLLLLLLL");
            setContentView(R.layout.settings);
        }

    }

【问题讨论】:

    标签: android


    【解决方案1】:

    只需将以下代码写入onConfigurationChanged方法并测试

    if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){
    
        Log.e("On Config Change","LANDSCAPE");
    }else{
    
        Log.e("On Config Change","PORTRAIT");
    }
    

    并像这样将android:configChanges="keyboardHidden|orientation" 写入您的清单文件中

    <activity android:name="TestActivity"
               android:configChanges="keyboardHidden|orientation">
    

    它在我身边工作,我希望它可以帮助你。

    如果您使用的是平板电脑,请将 |screenSize 添加到 android:configChanges

    【讨论】:

    • 如果您使用的是平板电脑,请将 |screenSize 添加到 android:configChanges
    【解决方案2】:

    您还应该注意:

    注意:从 Android 3.2(API 级别 13)开始,“屏幕尺寸” 当设备在纵向和横向之间切换时也会发生变化 方向。因此,如果您想防止运行时重新启动 为 API 级别 13 或更高级别(如 由 minSdkVersion 和 targetSdkVersion 属性声明),你 除了“方向”之外,还必须包含“screenSize”值 价值。也就是说,你必须 decalare android:configChanges="orientation|screenSize"。但是,如果您的 应用程序以 API 级别 12 或更低级别为目标,那么您的活动始终 自行处理此配置更改(此配置更改 不会重新启动您的活动,即使在 Android 3.2 或 更高的设备)。

    【讨论】:

      【解决方案3】:

      问题在于android:configChanges="orientation|keyboardHidden"

      如果您从 androidmanifest.xml 中删除 |keyboardHidden,则仅当您从横向旋转到纵向时才会触发 onConfigurationChanged,而不是当您从纵向转到横向时(至少在默认模拟器中)。

      希望这会有所帮助。

      【讨论】:

        【解决方案4】:

        除了上述答案之外:如果您在更新清单文件的同时覆盖 onConfigurationChanged 方法,则表示您正在自己处理 configurationchanges。因此添加:

        setContentView(R.layout.layout.xml);
        

        在您的 onConfigurationChanged 方法中 - 其中 layout 是您的布局文件 - 将解决问题。

        【讨论】:

          【解决方案5】:

          在您的活动文件中编写以下代码。

          @Override
          public void onConfigurationChanged(Configuration newConfig) {
              super.onConfigurationChanged(newConfig);
          
              if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){
               Log.e("On Config Change","LANDSCAPE");
              }
              else{
               Log.e("On Config Change","PORTRAIT");
              }
          
          }
          

          在你的清单文件中也写下如下代码:

          <activity
                  android:name=".activities.TestActivity"
                  android:configChanges="orientation|screenSize|keyboardHidden"
          />
          

          【讨论】:

            【解决方案6】:

            在android Manifest.xml文件中写下这一行:

            <activity
              android:name="MyActivity"
              android:configChanges="orientation|keyboardHidden" />
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2013-07-26
              • 2011-09-21
              • 2013-05-18
              • 1970-01-01
              相关资源
              最近更新 更多