反向肖像在android中不起作用

时间:2016-07-22 06:23:23

标签: android android-orientation

正如标题所说的那样,

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);

或在清单中声明

android:screenOrientation="reversePortrait"

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

1 个答案:

答案 0 :(得分:3)

试试这段代码。

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"/>