1

下面是我的代码......我遇到的问题是,当相机从iphone设备拍摄照片时,labelview可能隐藏在imageview后面......

var win = Titanium.UI.createWindow({
        title : 'Photoshare',
        fullscreen : false,
        barColor : '#000000',
        backgroundColor : '#fff'
    });
    // creating scroll view
    var scrollView = Titanium.UI.createScrollView({
        contentWidth : 'auto',
        contentHeight : 'auto',
        width : Titanium.Platform.displayCaps.platformWidth,
        height : Titanium.Platform.displayCaps.platformHeight,
        top : 0,
        showVerticalScrollIndicator : false,
        showHorizontalScrollIndicator : false,
        minZoomScale : 0.1,
        maxZoomScale : 100
    });
    // creating parent view to contain imageview
    var parentView = Titanium.UI.createView({
        width : Titanium.Platform.displayCaps.platformWidth,
        height : Titanium.Platform.displayCaps.platformHeight,
        top : 0
    });
    scrollView.add(parentView);
    // adding parent view to window
    //  win.add(parentView);

    // creating image view
    var imgView = Titanium.UI.createImageView({
        width : Titanium.Platform.displayCaps.platformWidth,
        height : Titanium.Platform.displayCaps.platformHeight,
        top : 0
    });
    // adding imageview to parent view
    parentView.add(imgView);
    var labelView = Titanium.UI.createView({
        top : 280,
        right : 0,
        width : 200,
        height : 100,
        backgroundColor : '#000',
        opacity : 0.5
    });
    imgView.add(labelView);
    // opening the camera at the start of the app
    Titanium.Media.showCamera({
        saveToPhotoGallery : false,
        allowEditing : false,
        mediaTypes : [Ti.Media.MEDIA_TYPE_PHOTO],
        success : function(event) {
            var capturedImage = event.media;
            imgView.image = capturedImage;
        },
        cancel : function() {
            scrollView.hide();
        },
        error : function(error) {
            if (error.code == Titanium.Media.NO_CAMERA) {
                alert('Please Run it on device');
            }

        },
    });
    var currentLocationLabel = Titanium.UI.createLabel({
        left : 5,
        //  top : 2,
        width : 'auto',
        height : 15,
        color : '#fff',
        font : {
            fontSize : 12
        },
    });
    //  labelView.add(currentLocationLabel);
    var previousLocationLabel = Titanium.UI.createLabel({
        left : 5,
        //  top : 66,
        width : 'auto',
        height : 15,
        color : '#fff',
        font : {
            fontSize : 12
        },
    });

    var distanceLabel = Titanium.UI.createLabel({
        left : 5,
        //  top : 50,
        width : 'auto',
        height : 15,
        color : '#fff',
        font : {
            fontSize : 12
        },
    });
    var timeLabel = Titanium.UI.createLabel({
        left : 5,
        //  top : 18,
        width : 'auto',
        height : 15,
        color : '#fff',
        font : {
            fontSize : 12
        },
    });
    var weatherLabel = Titanium.UI.createLabel({
        left : 5,
        //  top : 34,
        width : 'auto',
        height : 15,
        color : '#fff',
        font : {
            fontSize : 12
        },
    });
    win.addEventListener('focus', function(e) {
        var setTop = 2;
    /*  if (Ti.App.DataStorage.GetPreviousLocationVisibility() == 0) {
            labelView.remove(previousLocationLabel);
        } else {
            labelView.add(previousLocationLabel);
        }*/
        if (Ti.App.DataStorage.GetCurrentLocationVisibility() == 0) {
            labelView.remove(currentLocationLabel);
        } else {
            currentLocationLabel.setTop(setTop);
            labelView.add(currentLocationLabel);
            setTop = setTop + 15;
        }

        if (Ti.App.DataStorage.GetTimeVisibility() == 0) {
            labelView.remove(timeLabel);
        } else {
            timeLabel.setTop(setTop);
            labelView.add(timeLabel);
            setTop = setTop + 15;
        }
        if (Ti.App.DataStorage.GetWeatherVisibility() == 0) {
            labelView.remove(weatherLabel);
        } else {
            weatherLabel.setTop(setTop);
            labelView.add(weatherLabel);
            setTop = setTop + 15;
        }
        if (Ti.App.DataStorage.GetDistanceVisibility() == 0) {
            labelView.remove(distanceLabel);
        } else {
            distanceLabel.setTop(setTop);
            labelView.add(distanceLabel);
            setTop = setTop + 15;
        }
    });

任何人都可以告诉我我做错了什么......需要帮助

4

1 回答 1

2

一个潜在的问题可能是这样的:

 imgView.add(labelView);

将视图添加到 ImageView 具有未定义的行为,它被视为非容器视图。对我来说,这导致我的视​​图随机不显示,或者被放置在一个奇怪的位置。解决方案是创建一个容器视图,将 imgView 放在其中,然后将 labelView 放在它上面。

文档对此进行了晦涩的提及,但它让我很长时间没有想到并引起了很多头痛,指的是 ImageViews 和其他非容器视图:

Adding children to the these views may be supported on some platforms, 
but is not guaranteed to work across platforms. Where it is supported, 
it may not work as expected.

一般规则是始终检查并确保您调用的组件add支持它。

于 2013-02-21T07:18:34.017 回答