2013-11-20 20 views
0

我需要在屏幕上放置一個6x10單元矩陣。我發現我可以創建一個文本字段數組,我如何爲每個元素的頂部和左側分配值?帶鈦的TextField數組

爲清楚起見,請進行編輯:我需要將這些單元顯示爲每行6個單元的10行。

var textFields2 = []; 
for (var i = 0; i < 59; i++) 
{ 

    textFields2[i] = Ti.UI.createTextField({ 
     borderStyle : Titanium.UI.INPUT_BORDERSTYLE_ROUNDED, 
     width : '30dp', 
     height : '45dp', 
     //value : '', 
     top : '5%', // starting row 
     color : '#000000', 
     left : '0%' // starting column 
    }); 
} 

回答

0

嗯,做任何你需要將它們添加到容器。您還需要正確設置寬度和高度,以便它們適合屏幕,10行5個文本字段意味着每個文本字段的高度必須爲10%,寬度爲20%。然後,您可以使用水平佈局屬性來創建網格。

這裏是一個多爲自包含的,可能不是沒有缺陷例如:

var main = Ti.UI.createWindow(); 

var container = Ti.UI.createView({ 
    layout : 'horizontal', 
    horizontalWrap : true, // this is the default 
    width : Ti.UI.FILL, 
    height: Ti.UI.FILL 
}); 

// Set this and then calculate below 
var rows = 10, columns = 5; 

// Define the base attributes of a textfield 
var baseAttrs = { 
    borderStyle : Titanium.UI.INPUT_BORDERSTYLE_ROUNDED, 
    width : (100/columns) + '%', 
    height : (100/rows) + '%', 
    color : '#000000' 
}; 

// Add textfields to the container with the base attributes 
for(var i = 0; i < rows * columns; i++) { 
    var tfield = Ti.UI.createTextField(baseAttrs); 
    // Set an initial value 
    tfield.value = i; 
    container.add(tfield); 
} 

// Add container and open the window 
main.add(container); 
main.open(); 
+0

感謝。我試圖強制每6個元素的行增量...現在它顯示6行10個元素每個。 – Jocala

+0

因此,改變行和列變量....如果這有助於接受答案。 –

+0

是的,感謝我的第一個鈦應用程序kickstart。我最終將矩陣設置爲兩個類似電子表格的頁面的標籤滾動視圖容器x 2。很棒。 – Jocala