【问题标题】:Titanium Javascript - code works on iPhone, but not on Android?Titanium Javascript - 代码适用于 iPhone,但不适用于 Android?
【发布时间】:2011-12-27 12:06:39
【问题描述】:

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

【问题讨论】:

  • 你看到一个空白窗口吗?
  • 你试过用 Ti.UI.FILL 和 Ti.UI.SIZE 代替 "window.height" 吗?

标签: javascript android iphone ios titanium


【解决方案1】:

问题是您依赖于 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();

编辑:您可以做的另一件事是等到窗口打开,然后在评估宽度和高度后将滚动视图添加到其中:

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

});

【讨论】:

    【解决方案2】:

    我对钛或手机开发一无所知,但是您在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 */
    

    【讨论】:

    • 不需要分号
    • 你说得对,分号不是必需的,它们更像是语句分隔符,而不是强制性的语句终止符。尽管如此,我认为总是添加它们是一种很好的风格。谢谢,学到了新东西:)
    【解决方案3】:

    在地址栏中,输入“about:debug”。然后你会在设置菜单中看到许多额外的设置,应该说“显示 JavaScript 控制台”。检查这个,它可能会显示您遇到的错误。 (如果你安装了sdk,你也可以查看logcat。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多