尝试创建具有动态高度的视图时遇到了一些问题。
“post_comment”是我将显示文本的标签。文本的长度必须是动态的,并且“containComment”视图必须调整其高度才能显示此标签。
var post_comment = Titanium.UI.createLabel({
color:'#000',
text:L(randomText),
top:10,
bottom:30,
left:4,
width:220,
height:'auto',
font: { fontSize:11 },
});
var comment_height = 100;
post_comment.addEventListener('postlayout', function(e) {
comment_height = e.source.rect.height;
alert(comment_height); <--------- 1
});
var containComment = Titanium.UI.createView({
layout :'horizontal',
contentWidth:'100%',
height: comment_height,
});
alert(comment_height); <-------- 2
我使用 postlayout 来获取高度,但由于某种原因,我无法在函数之外获得相同的值。
我在箭头指示的 2 个位置测试了“comment_height”。在 1 中,显示的高度是正确的。但是在 2 处,高度始终是默认值 100。我认为这是由于“comment_height”不是全局的,所以我将它发送到脚本的头部,但它仍然不能解决问题。
任何帮助,将不胜感激。