我有一个应用程序,我试图在android和iphone上开发sencha touch 2框架。我想要的应用程序方向设置为纵向只有一个视图除外。如果设备方向是“纵向”,此视图将加载纵向视图;如果设备方向是“横向”,则此视图将加载横向视图。
目前,我在android上使用的是phonegap,我已经将方向设置为纵向。
但在创建视图时,我完全迷失了方向,该视图将根据方向加载“纵向视图”或“横向视图”。另外,如果我在phonegap中将方向粘贴到肖像,是否会检测到方向?
感谢您的帮助
发布于 2012-06-13 13:59:47
在我的问题没有任何答案的情况下,我会对自己回答。
例如,在Android (phonegap)上,在清单中将方向定义为:
android:screenOrientation=“传感器”
然后,在活动中启动应用程序之前,如果您希望应用程序仅使用纵向,请将其设置为Portrait : 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将其重新设置为PORTRAIT。
希望对大家有所帮助!
发布于 2014-03-02 10:40:19
在您希望标准方向的一个视图上,添加一个像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);
}
});https://stackoverflow.com/questions/10106390
复制相似问题