Titanium Javascript - 代码适用于iPhone,但不适用于Android?

时间:2011-12-27 12:06:39

标签: javascript android iphone ios titanium

我目前正在创建一个模块,它通过scrollviews为您完成了艰苦的工作。当我在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();

2 个答案:

答案 0 :(得分:4)

问题是你依赖于window.width和window.height,这些在Android上评价为0(而在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();

编辑:你可以做的另一件事是等到窗口打开,然后在评估宽度和高度后将scrollview添加到它:

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'
    }));

});

答案 1 :(得分: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 */