【问题标题】:How to lock orientation in some views in Titanium app如何在 Titanium 应用程序的某些视图中锁定方向
【发布时间】:2013-10-07 20:10:39
【问题描述】:

我正在为 Android 和 IOS 开发应用程序。在这个应用程序中,我有一个窗口,我可以添加/删除包含内容的不同视图。

我希望第一个视图仅处于纵向模式,而其余视图可以处于任何方向。

我该怎么做?

使用 Titan SDK 3.1.2,它或多或少可以在 IOS 上运行:

我的窗口:

var appWindow = Titanium.UI.createWindow({    
    top : 0,
    left : 0,
    height : utils.getScreenHeight(),
    width : utils.getScreenWidth(),    
    backgroundColor : "#393a3a",
    //fullscreen : true,    
    orientationModes : [Ti.UI.PORTRAIT, Ti.UI.UPSIDE_PORTRAIT],    
});

然后,当我要加载视图时:

var openWindow = function(e) {    
    appWindow.orientationModes = [Ti.UI.PORTRAIT, Ti.UI.UPSIDE_PORTRAIT, Ti.UI.LANDSCAPE_LEFT, Ti.UI.LANDSCAPE_RIGHT];

    if (e.win == 'Home') {
        Titanium.UI.orientation = Titanium.UI.PORTRAIT;
        appWindow.orientationModes = [Titanium.UI.PORTRAIT];
        orientacion = 0;
        activeView = Home.Constructor(appWindow);
    } else if (e.win == 'configuracion') {
        Titanium.UI.orientation = Titanium.UI.PORTRAIT;
        orientacion = 0;
        appWindow.orientationModes = [Titanium.UI.PORTRAIT];
        activeView = Configuracion.Constructor(appWindow);
    } else if (e.win == 'Circle') {
        activeView = Circle.Constructor(appWindow);
    }
    appWindow.add(activeView);
};

现在,我想使用 SDK 3.1.3 来支持 IOS 7,但它不起作用,所有视图都不允许旋转。

你知道我该怎么做吗?

非常感谢

【问题讨论】:

    标签: android view titanium ios7 orientation-changes


    【解决方案1】:

    我也没有在运行时更改 windowsorientationModes 的运气。文档说窗口属性orientationModes 及其方法setOrientationModes 必须在打开窗口之前设置

    所以从视图切换到窗口不是一种选择?

    如果您希望窗口允许与 tiapp.xml 中定义的方向不同的方向,则必须在每个窗口上单独设置orientationModes:

    // This window allows orientations defined in tiapp.xml
    var appWin = Ti.UI.createWindow({
        backgroundColor = '#FFF'
    });
    appWin.open();
    
    // This window is portrait only
    var win = Ti.UI.createWindow({
        backgroundColor: '#FFF',
        orientationModes: [Ti.UI.PORTRAIT]
    });
    win.open();
    
    // If this window is opened it allows multiple orientations
    var newWindow = Ti.UI.createWindow({
        backgroundColor: '#FFF',
        orientationModes: [Ti.UI.PORTRAIT, Ti.UI.LANDSCAPE_LEFT, Ti.UI.LANDSCAPE_RIGHT]
    });
    newWindow.open()
    

    这是使用 SDK 3.1.3 测试的。


    编辑
    要在 Android 上使用此功能,您必须通过设置 fullscreen: truenavBarHidden: true 来强制使用重量级窗口

    【讨论】:

    • 我在ios上试过了,效果很好。但是在Android上它不起作用。我还需要放什么吗?非常感谢
    • 对于 Android 窗口,您可以尝试设置 fullscreen: true 以强制使用重量级窗口。
    • 我不想在 Android 中显示标题栏,所以我需要全屏:false。最后,我解决了这个问题:全屏:false,navBarHidden:true,当我创建窗口时,然后:winPortrait.orientationModes = [Ti.UI.PORTRAIT, Ti.UI.UPSIDE_PORTRAIT];
    • 很高兴你能解决它,基本上navBarHidden : true和全屏效果一样。我会将其添加到答案中。
    【解决方案2】:

    您可以创建自定义AndroidManifest.xml 并为您想要的activities 设置screenOrientation 属性。-

    android:screenOrientation="portrait"
    

    更多信息here

    【讨论】:

    • 好的,但是,我只想在某些视图中锁定纵向方向,而在其他视图中允许任何方向。我认为android清单会影响所有应用程序,不是吗?我想要它用于Android和IOS。谢谢
    • 没试过,但我猜如果你不为Activity设置screenOrientation,它会不断改变方向行为。
    【解决方案3】:

    使用钛 SDK 3.1.2,我将以下代码用于:

    Titanium.UI.orientation = Titanium.UI.PORTRAIT;
    appWindow.orientationModes = [Titanium.UI.PORTRAIT];
    

    然后,当你加载其他视图时,然后输入:

    appWindow.orientationModes = [Ti.UI.PORTRAIT, Ti.UI.UPSIDE_PORTRAIT, Ti.UI.LANDSCAPE_LEFT, Ti.UI.LANDSCAPE_RIGHT];
    

    【讨论】:

    • 这就是我现在所拥有的。它不适用于 SDK 3.1.3,我想使用 SDK 3.1.3,因为我在使用 ios 7 时遇到了一些问题。谢谢
    猜你喜欢
    • 1970-01-01
    • 2017-10-03
    • 1970-01-01
    • 1970-01-01
    • 2016-03-22
    • 1970-01-01
    • 2019-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多