【问题标题】:Handle Screen Orientation?处理屏幕方向?
【发布时间】:2017-11-17 07:14:25
【问题描述】:

页面加载时,首先以 LANDSCAPE_MODE 加载,几秒钟后方向更改为 PORTRAIT_MODE 值

protected void onCreate(Bundle savedInstanceState)
{

    super.onCreate();

    setContentView(R.layout.layout);

    if(orientation.equals("true")) 
    {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
    else
    {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }
}

【问题讨论】:

  • 比什么问题
  • 检查 API 在 PREVIOUS 活动或 APPLICATION 类上的工作。
  • @UmairI 我认为您需要再次重新考虑 Activity 生命周期,因为在 onStart() developer.android.com/guide/components/activities/…987654321@ 之前调用了 onCreate()
  • @Shainu 将 android:screenOrientation 设置为 user 并在跳转到活动之前更新您的方向偏好:user = 用户当前的首选方向。更多信息:developer.android.com/guide/topics/manifest/…
  • 谢谢@SaraTirmizi。这对我有用。 :)。

标签: android orientation screen-orientation oncreate landscape-portrait


【解决方案1】:

默认情况下,无论设备处于当前方向,它都会在该模式下打开活动。没有特定的方法来处理这个

由于您在活动中以编程方式更改方向,该活动仅在活动onCreate() 方法之后执行

所以你每次都会遇到故障。我在这里遇到过类似的方向问题 Splash Screen Image for landscape and Portrait using Theme

【讨论】:

    【解决方案2】:

    @Shainu 设置

     android:screenOrientation = "user"
    

    在跳转到活动之前更新您对方向的偏好。 用户当前的首选方向。

    欲了解更多信息:https://developer.android.com/guide/topics/manifest/activity-element.html

    【讨论】:

      【解决方案3】:

      对我来说最好的解决方案是在调用 super.onCreate(); 之前处理屏幕方向;

      【讨论】:

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