图像视图内的视图隐藏在imageview后面

时间:2013-02-21 06:43:59

标签: titanium

下面是我的代码......我遇到的问题是,当相机从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;
        }
    });

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

1 个答案:

答案 0 :(得分:2)

潜在的问题可能是:

 imgView.add(labelView);

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

The docs make an obscure mention of this,但很长一段时间它逃脱了我并引起了很多麻烦,引用了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上的组件支持它。