android - 像在相机应用程序中一样检测方向

标签 android android-orientation

我正在开发某种类似于相机的应用程序,我希望该应用程序始终处于横向模式,我通过以下方式实现:

android:screenOrientation="portrait"

但我需要知道用户何时将手机旋转到横向模式,以便我可以将图标旋转 90 度。

我试过:

   if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) 
    {
        Toast.makeText(getApplicationContext(), "ORIENTATION_LANDSCAPE", Toast.LENGTH_SHORT).show();

    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){

        Toast.makeText(getApplicationContext(), "ORIENTATION_PORTRAIT", Toast.LENGTH_SHORT).show();
    }

但它只在我添加时有效:

android:configChanges="keyboardHidden|orientation|screenSize"

它还会自动改变我的方向。

那么我怎样才能让我的应用程序始终处于横向模式并且只检测手机何时处于纵向模式而手机不会自动更改任何内容?就像在相机应用程序中一样

最佳答案

使用 android:configChanges="keyboardHidden|orientation|screenSize" 是个好主意。

现在尝试手动设置方向:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

关于android - 像在相机应用程序中一样检测方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20855769/

相关文章:

android - 屏幕方向横向颠倒 - 为什么?

android - 如何将 query() 与 WHERE 子句一起使用?

Chrome 上使用 ARC 的 Android 应用程序,启用后退按钮在顶部

Android 4.4 进度对话框的全屏模式

android - orientation change 变为 null 后的回调

Android:禁用方向更改堆栈

c# - 解析 XAML : "Integer types not allowed" errors for many attributes such as width 时出错

java - 谷歌地图 API v2 崩溃

android - 屏幕锁定时 Activity 返回纵向

Android:仅在相机预览中旋转叠加按钮