我正在开发一个Xamarin Android应用程序,它大部分都是在肖像模式下运行的。然而,现在我们添加的功能,如果用户旋转设备,他们得到另一个视图在景观模式。
我的问题是,当我尝试使用以下代码片段时,无论我如何旋转设备,旋转总是具有值SurfaceRotation.Rotation0 (即0)。
var windowManager = Application.Context.GetSystemService(Context.WindowService).JavaCast<IWindowManager>();
var rotation = windowManager.DefaultDisplay.Rotation; // Always gives zero我正在FragmentView中执行这段代码(从基类继承,从MvxFragment继承)。我已经设法让一些代码使用OrientationEventListener工作,如下所示,但这并不理想。我想利用默认的Android行为来旋转设备,如果可能的话,不要定义我自己的角度范围:
// This works!
public override void OnOrientationChanged(int orientation)
{
if (app.IsPortrait && ((orientation >= 85 && orientation <= 95) || (orientation >= 265 && orientation <= 275)))
{
Mvx.Trace("Send message to open new view in landscape mode");
}
else if (!app.IsPortrait && (orientation < 85 || (orientation > 95 && orientation < 265 ) || orientation > 275))
{
Mvx.Trace("Send message to close the landscape view");
}
}我正在使用连接的LGE 5设备和三星GT-19300进行测试,结果是相同的。这两个设备都启用了自动旋转屏幕。我确实有一个UI (与在这个线程中问题相反)。我增加了
android:configChanges="orientation"去我的舱单。每个视图在声明活动时都使用正确的方向设置(例如使用ScreenOrientation = ScreenOrientation.Portrait)。我错过了什么吗?
提前感谢!
大卫
发布于 2014-07-30 16:15:43
我意识到,当您通过设置ScreenOrientation = ScreenOrientation.Portrait (例如)来指定活动的方向时,旋转(windowManager.DefaultDisplay.Rotation)总是为零,因为屏幕内容没有相对于设备旋转--不管设备的物理方向如何。
因此,为了解决我的问题,我删除了活动的ScreenOrientation设置。这意味着当设备被旋转时,内容也会被旋转(根据默认的Android行为),当这种情况发生时,DefaultDisplay.Rotation的值将按预期设置。这允许我在我的OrientationEventListener,OnOrientationChanged事件中使用以下代码:
if (app.IsPortrait && (rotation == ScreenOrientation.Landscape || rotation == ScreenOrientation.ReverseLandscape))
{
Mvx.Trace("Send message to open new view in landscape mode");
}
else if (!app.IsPortrait && (rotation == ScreenOrientation.Portrait || rotation == ScreenOrientation.ReversePortrait))
{
Mvx.Trace("Send message to close the landscape view");
}大卫
https://stackoverflow.com/questions/25037093
复制相似问题