Android - 以编程方式旋转屏幕而不锁定它 [英] Android - Rotate screen programmatically without locking it

查看:57
本文介绍了Android - 以编程方式旋转屏幕而不锁定它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力实现这种行为:

I'm trying to achieve this behaviour:

  • 当我将设备旋转到横向时,屏幕也会这样做.
  • 要将其恢复为纵向,有两种方法:
  • When I rotate the device to landscape, the screen does it too.
  • To get it back to portrait there are two ways:
  1. 旋转设备
  2. 点击一个仅在横向模式下出现的按钮,将屏幕旋转回纵向.

问题:将屏幕恢复为纵向的按钮工作正常,但随后我希望能够将设备旋转为横向并旋转屏幕,但它仍然锁定为纵向.

The problem: The button that puts the screen back to portrait works just fine but then I want to be able to rotate the device to landscape and rotate the screen, but it remains locked in portrait.

行为就像 youtube 播放器,您可以在其中旋转或单击按钮退出全屏.

The behaviour is like youtube player, where you rotate or click the button to exit fullscreen.

我的按钮代码:

findViewById(R.id.exit_fs).setOnClickListener(new Button.OnClickListener(){
    @Override
    public void onClick(View arg0) {
        setRequestedOrientation(Build.VERSION.SDK_INT < 9 ?
        ActivityInfo.SCREEN_ORIENTATION_PORTRAIT :
        ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
    }
});

有什么想法吗?

推荐答案

我发现这种方法非常好:

I'v found this approach very nice:

// E.g. fullscreen button pressed - set landscape orientation;
// it will disable orientation by the sensor.
setRequestedOrientation(SCREEN_ORIENTATION_LANDSCAPE)

// Assume now user rotated the device... or not.

// Anyway enable orientation by the sensor after few seconds, so user may use app normal way.
someView.postDelayed(
    { setRequestedOrientation(SCREEN_ORIENTATION_SENSOR) }, delay = 5000L)

这篇关于Android - 以编程方式旋转屏幕而不锁定它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆