【问题标题】:android:screenOrientation=”locked” on old OS versionsandroid:screenOrientation=”locked” 在旧操作系统版本上
【发布时间】:2014-01-26 17:00:50
【问题描述】:

即使设备旋转,我希望我的活动保持它开始时的方向,无论是纵向还是横向。 API 级别 18 引入了 android:screenOrientation=”locked” 应该可以完成这项工作:Locks the orientation to its current rotation, whatever that is.

但是,我还想支持较旧的操作系统版本。我怎样才能做到这一点?

我知道Lock screen orientation (Android) 讨论了横向模式下的锁定活动,How do I disable orientation change on Android? 讨论了纵向模式下的锁定活动(这在三星 Galaxy S、OS 2.3.3 上根本不适合我)。我也知道advice recommending not to do this

【问题讨论】:

    标签: android android-activity screen-orientation android-orientation


    【解决方案1】:

    MainActivity.java

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        if (getResources().getConfiguration().orientation == Configuration. ORIENTATION_LANDSCAPE)
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        else
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    
        // ...
    }
    

    我还修改了清单文件。但是,我不确定是否需要进行这些更改,至少 android:screenOrientation="unspecified" 无论如何都是默认的 screenOrientation 值。 AndroidManifest.xml

    <activity android:name="MainActivity"
        android:configChanges="keyboardHidden|orientation|screenSize"
        android:screenOrientation="unspecified" />
    

    【讨论】:

      猜你喜欢
      • 2014-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-05
      • 1970-01-01
      • 2013-01-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多