【发布时间】:2012-03-07 06:23:42
【问题描述】:
我通过在DefaultActivity 中的super.loadURL... 行之前添加super.setIntegerProperty("splashscreen", R.drawable.splash); 行,在我的phonegap 应用程序中添加了一个启动屏幕。
有没有办法只锁定初始屏幕的方向为纵向而不锁定整个应用程序?
【问题讨论】:
标签: android cordova splash-screen
我通过在DefaultActivity 中的super.loadURL... 行之前添加super.setIntegerProperty("splashscreen", R.drawable.splash); 行,在我的phonegap 应用程序中添加了一个启动屏幕。
有没有办法只锁定初始屏幕的方向为纵向而不锁定整个应用程序?
【问题讨论】:
标签: android cordova splash-screen
经过多次搜索,我发现解决初始屏幕方向的正确方法是使用此代码管理 两个图像 到您的 MainActivity
if (getResources().getConfiguration().orientation == 2) {
super.setIntegerProperty("splashscreen", R.drawable.splashlandscape);
Log.d("Orientation", "Landscape");
}
else {
super.setIntegerProperty("splashscreen", R.drawable.splashportrait);
Log.d("Orientation", "Portrait");
}
【讨论】:
以下解决方案适用于 Android 和 iOS 上的 Cordova 3+。它仅锁定初始屏幕的方向,然后在应用运行后解锁。
修复 config.xml 中的方向:
<preference name="orientation" value="portrait" />
然后在应用初始化后使用screen orientation plugin 解锁它:
document.addEventListener("deviceready", function(){
screen.unlockOrientation();
}, false);
【讨论】:
如果您使用的是最新版本的 PhoneGap (DroidGap) 或 Apache Cordova,您可以通过更改 Android.xml 文件中的 screenOrientation 来强制屏幕方向为横向。生成的 DroidGap “实例化活动”应如下所示:
<activity
android:name="org.apache.cordova.DroidGap"
android:label="@string/app_name"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="landscape"
>
<intent-filter>
</intent-filter>
</activity>
【讨论】:
在启动画面活动的 onCreate 方法中写入以下内容:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
【讨论】:
在您的 AndroidManifest.xml 中添加这一行:
<activity android:name="SplashScreen" android:screenOrientation="portrait"></activity>
【讨论】: