2012-04-11 69 views
0

我有一个应用程序,我尝试用android和iphone上的sencha touch 2框架开发。 我希望将应用程序方向设置为仅用于纵向,除了一个视图。 如果设备方向是“纵向”或横向视图,如果设备方向是“横向”,则此视图将载入纵向视图sencha touch 2 stick设备的方向除了风景1视图

目前,我在android上使用phonegap,并将方向固定为纵向。

但是,当它创建视图时,我很失落,它将根据方向加载“纵向视图”或“横向视图”。 如果我将手机的方向与手机中的肖像粘在一起,还会检测到方向吗?

感谢您的帮助

回答

1

没有从我的问题的任何答案我会回答自己。

在Android(PhoneGap的)例如,在清单中定义的取向为:

机器人:screenOrientation = “传感器”

然后,开始在活动中的应用程序之前,如果您希望应用程序只使用肖像将其设置为肖像: setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); super.loadUrl(“file:///android_asset/www/index.html”);

在你JS,如果你想获得横向/纵向使用,命令发送到主应用程序与动作调用自定义setOrientation代码:

onSetOrientationSuccess: function(result) { 
    ... 
}, 
onSetOrientationError: function(err) { 
    ... 
}, 

setOrientation: function(orientation) { 

    PhoneGap.exec(this.onSetOrientationSuccess, this.onSetOrientationError, "OrientationPlugin", orientation, []); 
}, 

OrientationPlugin仅仅是一个简单的PhoneGap插件在那里你会检查行动,并调用正确的代码:

public class OrientationPlugin extends Plugin { 

public static final String ACTION="undefined"; 

@Override 
public PluginResult execute(String action, JSONArray data, String callbackId) { 
    Log.d("OrientationPlugin", "Plugin called"); 
    PluginResult result = null; 
    YourActivity activity = (YourActivity) this.ctx; 
    Log.d("OrientationPlugin", "Plugin Got context"); 

    if (ACTION.equals(action)) { 
     Log.d("OrientationPlugin", "Plugin set SCREEN_ORIENTATION_SENSOR");   
     activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR); 
    } 
    else { 
     Log.d("OrientationPlugin", "Plugin set SCREEN_ORIENTATION_PORTRAIT");    
     activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);   
    } 
    Log.d("OrientationPlugin", "Plugin set ALL OK");    
    result = new PluginResult(Status.OK, "ok"); 
    return result; 
} 

}

那么你可以随时将其设置回纵向,当您需要它来自JS。

希望有所帮助!

0

在您希望标准方向的视图中,添加一个自定义值,如doLandscape: 1。我喜欢在app.config.Runtime类中设置运行时的“全局”值,否则如果它适用于整个应用程序,我会将它附加到导航栏。

Ext.define(‘MyApp.config.Runtime’,{ 
    singleton : true, 
    config : { 
     doLandscape: 0 // initialize to 0 
    }, 
    constructor : function(config){ 
     this.initConfig(config); 
    } 
}); 

在视口中,添加:

onOrientationChange: function(viewport, orientation, width, height) { 
    // test trigger and values 
    console.log('o:' + orientation + ' w:' + width + ' h:' + height); 

    if (orientation == 'landscape' && MyApp.config.Runtime.doLandscape === 0) { 
     // most often this is all that's needed to prevent change 
     return false; 

     // alternatively/additionally set it back to portrait manually. 
     viewport.orientation = this.PORTRAIT; 
    } 
} 

在更新的源代码看,这里是负责处理和火灾方位的变化视口默认代码。

onOrientationChange: function() { 
    var currentOrientation = this.getOrientation(), 
     newOrientation = this.determineOrientation(); 

    if (newOrientation !== currentOrientation) { 
     this.fireOrientationChangeEvent(newOrientation, currentOrientation); 
    } 
}, 

fireOrientationChangeEvent: function(newOrientation, oldOrientation) { 
    var clsPrefix = Ext.baseCSSPrefix; 
    Ext.getBody().replaceCls(clsPrefix + oldOrientation, clsPrefix + newOrientation); 

    this.orientation = newOrientation; 

    this.updateSize(); 
    this.fireEvent('orientationchange', this, newOrientation, this.windowWidth, this.windowHeight); 
} 

当你在本地包装,此选项可以作为另一种选择:

Ext.device.Orientation.on({ 
    scope: this, 
    orientationchange: function(e) { 
     console.log('Alpha: ', e.alpha); 
     console.log('Beta: ', e.beta); 
     console.log('Gamma: ', e.gamma); 
    } 
});