108

実行時に向きをロックする方法はありますか?たとえば、ユーザーが現在横向きになっている場合は、画面を横向きにロックしてメニューオプションを切り替えることができるようにします。

4

7 に答える 7

136
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

アクティビティで呼び出され、横向きにロックします。ActivityInfo クラスで他のフラグを探します。ポートレートに戻すか、センサー/スライダー駆動にすることができます。

詳細はこちら: http://www.devx.com/wireless/Article/40792

于 2010-03-02T21:06:37.093 に答える
109

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);
}
于 2012-05-07T19:36:34.483 に答える
52

これは、逆ポートレートと逆ランドスケープのデバイスで機能します。

ロックの向き:

    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);
于 2013-01-04T01:53:07.587 に答える
28

私も似たようなケースがあったようです。任意のオリエンテーションをサポートしたかったのですが、ワークフローの特定の時点の後、現在のオリエンテーションを維持する必要がありました。私の解決策は次のとおりです。

保護されたワークフローの開始時:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);

保護されたワークフローの終了時:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
于 2012-01-07T13:46:13.663 に答える
26

タブレットをサポートする @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);
}
于 2013-01-28T15:33:27.957 に答える
0

これは、上記の @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;
}

これは、使用する前に別のアプローチで行ったようにテストされていませんが、役立つ場合があります。

于 2015-08-09T11:10:41.807 に答える