実行時に向きをロックする方法はありますか?たとえば、ユーザーが現在横向きになっている場合は、画面を横向きにロックしてメニューオプションを切り替えることができるようにします。
7 に答える
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
アクティビティで呼び出され、横向きにロックします。ActivityInfo クラスで他のフラグを探します。ポートレートに戻すか、センサー/スライダー駆動にすることができます。
getConfigurationが返すものとsetRequestedOrientationが必要とするものの違いに注意してください。どちらもintですが、異なる定数定義から取得されます。
180度の反転を許可しながら、現在の方向をロックする方法は次のとおりです。
int currentOrientation = getResources().getConfiguration().orientation;
if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
}
else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
}
これは、逆ポートレートと逆ランドスケープのデバイスで機能します。
ロックの向き:
int orientation = getActivity().getRequestedOrientation(); int rotation = ((WindowManager) getActivity().getSystemService( Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation(); switch (rotation) { case Surface.ROTATION_0: orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; break; case Surface.ROTATION_90: orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; break; case Surface.ROTATION_180: orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT; break; default: orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE; break; } getActivity().setRequestedOrientation(orientation);
オリエンテーションのロックを解除します。
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
私も似たようなケースがあったようです。任意のオリエンテーションをサポートしたかったのですが、ワークフローの特定の時点の後、現在のオリエンテーションを維持する必要がありました。私の解決策は次のとおりです。
保護されたワークフローの開始時:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
保護されたワークフローの終了時:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
タブレットをサポートする @pstoppani の回答の代替 (@pstoppani の回答と同様に、これは 2.2 以上のデバイスでのみ機能し
ますSamsung Galaxy SIII
)Samsung Galaxy Tab 10.1
public static void lockOrientation(Activity activity) {
Display display = ((WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int rotation = display.getRotation();
int tempOrientation = activity.getResources().getConfiguration().orientation;
int orientation = 0;
switch(tempOrientation)
{
case Configuration.ORIENTATION_LANDSCAPE:
if(rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90)
orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
else
orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
break;
case Configuration.ORIENTATION_PORTRAIT:
if(rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_270)
orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
else
orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
}
activity.setRequestedOrientation(orientation);
}
これは、上記の @pstoppani の回答の Xamarin 変換です。
注: これはフラグメント用であり、アクティビティを置き換えます。これで。アクティビティ内で使用する場合。
public void LockRotation()
{
ScreenOrientation orientation;
var surfaceOrientation = Activity.WindowManager.DefaultDisplay.Rotation;
switch (surfaceOrientation) {
case SurfaceOrientation.Rotation0:
orientation = ScreenOrientation.Portrait;
break;
case SurfaceOrientation.Rotation90:
orientation = ScreenOrientation.Landscape;
break;
case SurfaceOrientation.Rotation180:
orientation = ScreenOrientation.ReversePortrait;
break;
default:
orientation = ScreenOrientation.ReverseLandscape;
break;
}
Activity.RequestedOrientation = orientation;
}
public void UnlockRotation()
{
Activity.RequestedOrientation = ScreenOrientation.Unspecified;
}
これは、使用する前に別のアプローチで行ったようにテストされていませんが、役立つ場合があります。