มีวิธีล็อคการวางแนวระหว่างรันไทม์หรือไม่? ตัวอย่างเช่นฉันต้องการอนุญาตให้ผู้ใช้ล็อกหน้าจอให้เป็นแนวนอนหากผู้ใช้อยู่ในแนวนอนและสลับตัวเลือกเมนู
มีวิธีล็อคการวางแนวระหว่างรันไทม์หรือไม่? ตัวอย่างเช่นฉันต้องการอนุญาตให้ผู้ใช้ล็อกหน้าจอให้เป็นแนวนอนหากผู้ใช้อยู่ในแนวนอนและสลับตัวเลือกเมนู
คำตอบ:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);เรียกว่ากิจกรรมจะล็อคให้อยู่ในแนวนอน มองหาแฟล็กอื่น ๆ ในคลาส ActivityInfo คุณสามารถล็อคกลับเป็นแนวตั้งหรือทำให้เซ็นเซอร์ / ตัวเลื่อนขับเคลื่อน
ข้อมูลเพิ่มเติมที่นี่: http://www.devx.com/wireless/Article/40792
โปรดระวังความแตกต่างระหว่างสิ่งที่ 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);
"Returns the rotation of the screen from its "natural" orientation."  ดังนั้นในโทรศัพท์ที่บอกว่า ROTATION_0 เป็นแนวตั้งน่าจะถูกต้อง แต่ในแท็บเล็ตการวางแนว "ธรรมชาติ" น่าจะเป็นแนวนอนและ ROTATION_0 ควรกลับเป็นแนวนอนแทนที่จะเป็นแนวตั้ง
                    ฉันดูเหมือนจะมีกรณีที่คล้ายกัน ฉันต้องการสนับสนุนการวางแนวใด ๆ แต่ฉันต้องอยู่ในทิศทางปัจจุบันหลังจากถึงจุดหนึ่งในเวิร์กโฟลว์ วิธีแก้ปัญหาของฉันคือ:
เมื่อเข้าสู่เวิร์กโฟลว์ที่มีการป้องกัน:
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);
}||ในและrotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90 rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_270ดังนั้นฉันจึงมีข้อสงสัย 2 ประการ :::: ประการแรกทำไมROTATION_0แทนที่จะเป็นROTATION_180ในกรณีที่สองและอีกประการหนึ่งทำไมต้องตรวจสอบ 0 องศาด้วย 90 ไม่ใช่ 180 ??
                    ||ตรวจสอบที่แตกต่างกันจึงจัดการการวางแนวเริ่มต้นที่เป็นไปได้สองแบบโดยอิงตามอุปกรณ์ที่รายงานแนวนอนเทียบกับแนวตั้ง
                    นี่คือรหัสของฉันคุณสามารถล็อคด้วยวิธีใดวิธีหนึ่งต่อไปนี้หน้าจอของคุณและเมื่อเสร็จงานแล้วจะปลดล็อกด้วยการปลดล็อก
/** Static methods related to device orientation. */
public class OrientationUtils {
    private OrientationUtils() {}
    /** Locks the device window in landscape mode. */
    public static void lockOrientationLandscape(Activity activity) {
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }
    /** Locks the device window in portrait mode. */
    public static void lockOrientationPortrait(Activity activity) {
    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
    /** Locks the device window in actual screen mode. */
    public static void lockOrientation(Activity activity) {
        final int orientation = activity.getResources().getConfiguration().orientation;
        final int rotation = ((WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation();
        // Copied from Android docs, since we don't have these values in Froyo 2.2
        int SCREEN_ORIENTATION_REVERSE_LANDSCAPE = 8;
        int SCREEN_ORIENTATION_REVERSE_PORTRAIT = 9;
        // Build.VERSION.SDK_INT <= Build.VERSION_CODES.FROYO
        if (!BuildVersionUtils.hasGingerbread()) {
            SCREEN_ORIENTATION_REVERSE_LANDSCAPE = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
            SCREEN_ORIENTATION_REVERSE_PORTRAIT = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
        }
        if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90){
            if (orientation == Configuration.ORIENTATION_PORTRAIT){
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            }
            else if (orientation == Configuration.ORIENTATION_LANDSCAPE){
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            }
        }
        else if (rotation == Surface.ROTATION_180 || rotation == Surface.ROTATION_270) 
        {
            if (orientation == Configuration.ORIENTATION_PORTRAIT){
                activity.setRequestedOrientation(SCREEN_ORIENTATION_REVERSE_PORTRAIT);
            }
            else if (orientation == Configuration.ORIENTATION_LANDSCAPE){
                activity.setRequestedOrientation(SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
            }
        }
    }
    /** Unlocks the device window in user defined screen mode. */
    public static void unlockOrientation(Activity activity) {
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);
    }
}นี่คือการแปลง Xamarin เป็นคำตอบของ @pstoppani ด้านบน
หมายเหตุ: สำหรับ Fragment แทนที่Activity ด้วยสิ่งนี้ หากใช้ภายในกิจกรรม
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;
}สิ่งนี้ยังไม่ผ่านการทดสอบเนื่องจากใช้วิธีการอื่นก่อนที่จะใช้ แต่อาจมีประโยชน์