Titanium 下如何禁止屏幕旋转 (disable orientation)
访问量: 3304
refer to: http://docs.appcelerator.com/titanium/latest/#!/guide/Orientation
觉得麻烦的话,也可以使用这个: http://developer.appcelerator.com/question/40001/android-force-orientation
注意: 奇怪的安卓
对于安卓的屏幕旋转, 有点奇怪, 你设置的跟你获得的结果不一样. 设置的时候, 有四个值: 头部在 上,下,左, 右. 但是查询屏幕方向时,只能获得: 水平/竖直.两种结果.
另外, 对于 安卓phone(初始位置是竖屏) , "top 0度": 指的是 竖屏: 头部朝上. top=270 指的是: 头部(横屏) 朝左 (landscape left)
对于安卓平板 (初始位置是横屏) : top = 0 时, 是横屏 头部朝左. top = 90时, 是竖屏头部朝上.
上面两行的top都指的是 sensor (位置感应器) 的度数 .
下面开始正文
支持屏幕变换是我们的机会. apple 是这样说的. 横屏竖屏变换的: 几个原则:
1. 对于iphone, 不要支持 竖屏向上 和竖屏向下 的变换
2. 对于iphone/ipod, 要么全部禁用屏幕变换,要么 react to orientation change.
3. 对于 ipad, 最好支持所有4种屏幕变换
上述原则对于android phone/pad 同样适用.
在Titanium 中, 禁止屏幕变换是通过 tiapp.xml 来修改的. ios 和 android 不太一样.
IOS
titanium 默认对于iphone 不支持旋转(仅仅竖屏朝上一种模式), 对于ipad支持4种模式
<?xml version="1.0" encoding="UTF-8"?> <ti:app xmlns:ti="http://ti.appcelerator.org"> <ios> <plist> <dict> <key>UISupportedInterfaceOrientations~iphone</key> <array> <string>UIInterfaceOrientationPortrait</string> </array> <key>UISupportedInterfaceOrientations~ipad</key> <array> <string>UIInterfaceOrientationPortrait</string> <string>UIInterfaceOrientationPortraitUpsideDown</string> <string>UIInterfaceOrientationLandscapeLeft</string> <string>UIInterfaceOrientationLandscapeRight</string> </array> </dict> </plist> </ios> </ti:app>
安卓
主要是修改 AndroidManifest.xml 这个文件. 它是由于 tiapp.xml 生成的. 所以,我们需要把它里面的 生成的代码copy出来:
把 build/android/AndroidManifest.xml 中的 application node 复制到 tiapp.xml 中去,并且 为每个 <activity> node 加上这个属性:android:screenOrientation="nosensor" (更多可用的值,见: http://developer.android.com/guide/topics/manifest/activity-element.html#screen )
复制好的 tiapp.xml 看起来如下:
<android xmlns:android="http://schemas.android.com/apk/res/android"> <manifest> <application android:icon="@drawable/icon" android:label="乐股在线" android:name="Application" android:debuggable="false" android:theme="@style/Theme.AppCompat.Translucent.NoTitleBar"> <activity android:screenOrientation="nosensor" android:name=".Activity" android:label="@string/app_name" android:theme="@style/Theme.Titanium" android:configChanges="keyboardHidden|orientation|screenSize"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <activity android:screenOrientation="nosensor" android:name="org.appcelerator.titanium.TiActivity" android:configChanges="keyboardHidden|orientation|screenSize"/> <activity android:screenOrientation="nosensor" android:name="org.appcelerator.titanium.TiTranslucentActivity" android:configChanges="keyboardHidden|orientation|screenSize" android:theme="@style/Theme.AppCompat.Translucent"/> <activity android:screenOrientation="nosensor" android:name="ti.modules.titanium.ui.android.TiPreferencesActivity" android:configChanges="screenSize"/> <activity android:screenOrientation="nosensor" android:name="ti.modules.titanium.media.TiCameraActivity" android:configChanges="keyboardHidden|orientation|screenSize" android:theme="@style/Theme.AppCompat.Translucent.NoTitleBar.Fullscreen"/> </application> </manifest> </android>
只针对某个 windows 设置 横屏竖屏:
注意下面是分别针对 IOS 和 安卓的 window一直适用 竖屏
var win = Ti.UI.createWindow({ /* on Android, it needs to be a "heavyweight" window */ fullscreen: false, /* This works on iOS */ orientationModes: [ Ti.UI.PORTRAIT, Ti.UI.UPSIDE_PORTRAIT ] }); // but for Android using Titanium prior to 2.1 you have to set it after creation win.orientationModes = [Ti.UI.PORTRAIT, Ti.UI.UPSIDE_PORTRAIT]
如果觉得以上太麻烦,就:
if (Ti.Platform.osname == 'android'){ Ti.Gesture.addEventListener('orientationchange', function(e) { Ti.Android.currentActivity.setRequestedOrientation(Ti.Android.SCREEN_ORIENTATION_PORTRAIT); }); }