メインの Phonegap Activity クラスを変更する必要があります。まず、AndroidManifest ファイルから次の行を削除します。
android:screenOrientation="portrait"
まだ必要なもの:
android:configChanges="orientation|keyboardHidden"
残念ながら、screenOrientation で複数の方向モードを構成することはできません。そのため、Java コードを使用して変更します。
これは実用的なコード例です:
package com.roadrunner.mobile21;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.Display;
import android.view.OrientationEventListener;
import android.view.Surface;
import com.phonegap.*;
public class RoadRunnerActivity extends DroidGap {
OrientationEventListener myOrientationEventListener;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.loadUrl("file:///android_asset/www/index.html");
myOrientationEventListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL){
@Override
public void onOrientationChanged(int arg0) {
Display display;
display = getWindow().getWindowManager().getDefaultDisplay();
int rotation = display.getRotation();
if(rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90) {
unlockScreenOrientation();
}
}
};
if (myOrientationEventListener.canDetectOrientation()){
myOrientationEventListener.enable();
} else{
finish();
}
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Display display;
display = getWindow().getWindowManager().getDefaultDisplay();
int rotation = display.getRotation();
if(rotation == Surface.ROTATION_180 || rotation == Surface.ROTATION_270) {
lockCurrentScreenOrientation();
}
}
private void lockCurrentScreenOrientation() {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
private void unlockScreenOrientation() {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);
}
}
- css を使用して画面の向きを変更することもできます。少しバグがあるので好きではありませんが、ここで詳細を見つけることができます。これは最も簡単な方法ですが、機能させるには少しプレイする必要があります。
これは簡単にできます:
$(window).bind("orientationchange", function(){
var orientation = window.orientation;
var new_orientation = (orientation) ? 0 : 180 + orientation;
$('body').css({
"-webkit-transform": "rotate(" + new_orientation + "deg)"
});
});
0 度と 90 度の方向のみに対応するように式を変更する必要があります。