2011-12-27 51 views
0

我目前正在製作一個模塊,它使用滾動視圖爲您完成了艱苦的工作。當我在iPhone模擬器上運行此代碼時,它非常完美 - 沒有錯誤。在Android模擬器上,我只看到一個空白的屏幕?Titanium Javascript - 代碼適用於iPhone,但不適用於Android?

爲什麼我只看到一個空白屏幕,我該如何解決? 這裏是我的代碼:

window = Ti.UI.createWindow({ 
    backgroundColor : 'white' 
}); 

function SuperScrollView(window) { 
    this.scrollview = Ti.UI.createScrollView({ 
     showVerticalScrollIndicator : true, 
     height : window.height, 
     width : window.width, 
     contentHeight : 'auto' 
    }); 
    this.view = Ti.UI.createView({ 
     height : 0, 
     width : window.width 
    }); 
    this.addElement = function(element) { 
     var height = this.view.height; 
     var elementHeight = element.height; 
     element.top = height; 
     this.view.add(element); 
     this.view.height += elementHeight; 
     height = this.view.height; 
     this.scrollview.contentHeight = height; 
     alert(height); 
    } 
    this.scrollview.add(this.view); 
    window.add(this.scrollview); 
} 

var scrollview = new SuperScrollView(window); 
scrollview.addElement(Ti.UI.createView({ 
    width : 200, 
    height : 500, 
    backgroundColor : 'blue' 
})); 

window.open(); 
+0

你看到一個空白窗口? –

+0

您是否試過Ti.UI.FILL和Ti.UI.SIZE而不是「window.height」? – Norris

回答

4

問題是你依賴於window.width和window.height,並且這些評估爲0(雖然它們在Android上工作正常iOS版)。這會導致您的視圖不具有任何大小。最好使用Ti.Platform.displayCaps.platformWidth和Ti.Platform.displayCaps.platformHeight來獲取屏幕的尺寸。你需要意識到,每個平臺上的情況都會有所不同,並且相應地調整你的觀點,但是你總會遇到這個問題。

我改變了你的代碼,所以你可以看到它在兩個平臺上的行動。

window = Ti.UI.createWindow({ 
    backgroundColor : 'white' 
}); 

function SuperScrollView(window) { 
    this.scrollview = Ti.UI.createScrollView({ 
     showVerticalScrollIndicator : true, 
     height : Ti.Platform.displayCaps.platformHeight, 
     width : Ti.Platform.displayCaps.platformWidth, 
     contentHeight : 'auto' 
    }); 
    this.view = Ti.UI.createView({ 
     height : 0, 
     width : Ti.Platform.displayCaps.platformWidth 
    }); 
    this.addElement = function(element) { 
     var height = this.view.height; 
     var elementHeight = element.height; 
     element.top = height; 
     this.view.add(element); 
     this.view.height += elementHeight; 
     height = this.view.height; 
     this.scrollview.contentHeight = height; 
     alert(height); 
    } 
    this.scrollview.add(this.view); 
    window.add(this.scrollview); 
} 

var scrollview = new SuperScrollView(window); 
scrollview.addElement(Ti.UI.createView({ 
    width : 200, 
    height : Ti.Platform.displayCaps.platformHeight + 500, 
    backgroundColor : 'blue' 
})); 

window.open(); 

編輯:你可以做的另一件事就是等到窗口已經打開,然後滾動視圖添加到它曾經的寬度和高度進行了評價:

window = Ti.UI.createWindow({ 
    backgroundColor : 'white' 
}); 

function SuperScrollView(window) { 
    this.scrollview = Ti.UI.createScrollView({ 
     showVerticalScrollIndicator : true, 
     height : window.height, 
     width : window.width, 
     contentHeight : 'auto' 
    }); 
    this.view = Ti.UI.createView({ 
     height : 0, 
     width : window.width 
    }); 
    this.addElement = function(element) { 
     var height = this.view.height; 
     var elementHeight = element.height; 
     element.top = height; 
     this.view.add(element); 
     this.view.height += elementHeight; 
     height = this.view.height; 
     this.scrollview.contentHeight = height; 
     alert(height); 
    } 
    this.scrollview.add(this.view); 
    window.add(this.scrollview); 
} 


window.open(); 

window.addEventListener('open', function(){ 
    var scrollview = new SuperScrollView(window); 
    scrollview.addElement(Ti.UI.createView({ 
     width : 200, 
     height : window.height + 500, 
     backgroundColor : 'blue' 
    })); 

}); 
0

我不知道鈦或手機開發任何東西,但你在this.addElement作業結尾缺少一個分號;

this.addElement = function(element) { 
    var height = this.view.height; 
    var elementHeight = element.height; 
    element.top = height; 
    this.view.add(element); 
    this.view.height += elementHeight; 
    height = this.view.height; 
    this.scrollview.contentHeight = height; 
    alert(height); 
}; /* <-- missing semicolon */ 
+0

不需要分號 – Eliasdx

+0

Javascript不需要分號:)但無論如何感謝.. –

+1

你說得對,不需要分號,它們更像是語句分隔符而不是強制語句結束符。儘管如此,我認爲總是添加它們是一種很好的風格。謝謝,一個新的東西瞭解到:) – Haes

0

在在地址欄中輸入「about:debug」。然後你會在設置菜單中看到很多額外的設置,應該說「顯示JavaScript控制檯」。選中它,它可能會顯示你有錯誤。 (如果你已經安裝了SDK,你也可以檢查logcat。)

相關問題