2016-03-31 138 views
0

我想问你,如何使用视图的“中心”属性(例如标签)。如果我设置了它,没有任何反应,相应的标签将自己设置到屏幕的中心,无论我将其设置为点x和y.:20在创建时,但我需要稍后将其居中。重新定位标签

我的代码

var width = Ti.Platform.displayCaps.platformWidth; 
var height = Ti.Platform.displayCaps.platformHeight; //create simple window 
var window = Ti.UI.createWindow({ 
     width:width, 
     height:height, 
     backgroundColor:"#ffffff" 
}); 
//add button to window with predefined left padding 
var button = Ti.UI.createButton({ 
     title:"Press me", 
     left:20, 
     color:"#000000" 
}); 
window.add(button); 
button.addEventListener("click", function() { 
    //this should set button position to center, but does nothing 
    button.setCenter(width/2, height/2); 
}); 
window.open(); 
+0

你能用你的代码编辑你的文章吗?谢谢 – Giordano

+0

var width = Ti.Platform.displayCaps.platformWidth; var height = Ti.Platform.displayCaps.platformHeight; //创建简单窗口 var window = Ti.UI.createWindow({ width:width, height:height, backgroundColor:“#ffffff” }); //添加按钮窗口与预定义的左填充 VAR键= Ti.UI.createButton({ 标题: “按我”, 左:20, 颜色: “#000000” }); window.add(button); button.addEventListener(“click”,function() { // this should set button position to center,but nothing nothing button.setCenter(width/2,height/2); }); window.open(); –

回答

0

只是猜测不用代码来看看,但什么是可能发生的事情是你的标签设置为width:Ti.UI.SIZE所以定心看起来好像没什么改变,因为标签是大小相同文字,你可能想要做的是设置width:Ti.UI.FILL或其他一些比你的文字大的宽度,然后你的标签将居中,因为标签的宽度将大于文本宽度。

+0

只是建立在Mike的答案基础上的一个提示:有时,它可以提供有用的信息,在相关元素上设置边框或背景颜色,以便您可以轻松查看它有多大。 –