2012-04-11 36 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); 
    } 
});