简体   繁体   English

将屏幕切换为横向或纵向,但不锁定方向

[英]Switch screen to landscape or portrait but don't lock orientation

I would like to accomplish something like this: 我想完成这样的事情:

  1. User is in portrait orientation 用户处于纵向
  2. User clicks a button and is transferred to landscape orientation 用户单击按钮,然后转到横向
  3. Orientation is not locked and if user rotates his phone, he is transferred again to portrait orientation. 方向未锁定,并且如果用户旋转手机,他将再次转到纵向。

What would be the correct way to do this? 正确的方法是什么? I am trying to lock the screen with the following command: 我正在尝试使用以下命令锁定屏幕:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);

And then immediately calling: 然后立即调用:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);

The problem is that this immediately transfers screen back to portrait mode. 问题在于这会立即将屏幕转换回纵向模式。 Screen just blinks for a half a second. 屏幕仅闪烁半秒钟。

Use can use in java code: 使用可以在Java代码中使用:

    @Override
    public void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);

         // requesting to turn the title OFF
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        // making it full screen
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);


        setContentView(R.layout.main);
}

You can also do in AndroidManifest.xml: 您也可以在AndroidManifest.xml中执行以下操作:

 <activity
            android:name="com.example.main"
            android:label="@string/app_name"
            android:screenOrientation="landscape" >

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM