反向肖像不适用于 android

reverse portrait not working in android

正如标题所说,当调用

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);

或在清单中声明

android:screenOrientation="reversePortrait"

根本不旋转屏幕。当我尝试使用反向景观时,它工作正常。这没有任何意义。我做错什么了吗?提前致谢。

试试这个代码。

public class MainActivity extends AppCompatActivity {

    @Override protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onResume() {
        super.onResume();
        Settings.System.putInt(getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0);// turn off rotation according to accelerometer
        Settings.System.putInt(getContentResolver(), Settings.System.USER_ROTATION, Surface.ROTATION_180);//reverse portrait
    }

    @Override
    protected void onPause() {
        super.onPause();
        Settings.System.putInt(getContentResolver(), Settings.System.USER_ROTATION, Surface.ROTATION_0);//portrait
        Settings.System.putInt(getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 1);//turn on rotation according to accelerometer
    }
}

不要忘记为 AndroidManifest.xml

添加权限
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>