2011-12-27 54 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。)

相关问题