0

エミュレートしようとするTi.UI.iPhone.SystemButton.FLEXIBLE_SPACEと問題が発生し、何か提案 (または気付いていないネイティブ機能) があるかどうか疑問に思いました。現時点では、ツールバーをエミュレートするための一時的な回避策があります。アイテムが 1 つある場合は中央に配置され、2 つある場合は最初のアイテムが左側に、2 つ目が右側に配置され、3 つ以上の場合はアイテムが中央に配置されます。最初でも最後でもない各アイテムは、ビューの特定のスロットに割り当てられます (スロットの幅はアイテムの数によって異なります)。以下の例では、オブジェクトitemsの配列であり、オブジェクトです。( Android の場合) は、対応する iOS の「定数」に対応する文字列です。Ti.UI.ButtonviewTi.Ui.ViewsystemButton

// Iterate over each item
for (var i in items)
{

    // Continue if a flexible space
    if ('FLEXIBLE_SPACE' === items[i].systemButton)
    {
        continue
    }

    // If there is more than one item
    if (1 !== items.length)
    {

        // Get the key
        var key = parseInt(i, 10)

        // Set the position
        switch (true)
        {

            // First
            case (0 === key):
                items[i].left = 0
                break

            // Last
            case (items.length - 1 === key):
                items[i].right = 0
                break

            // Otherwise if there are more than 3 items
            case (3 < items.length):

                // Get the slot width
                var slot = 100 / items.length

                // If a left position
                if (key < items.length / 2)
                {
                    items[i].left = (slot * key) + '%'
                }

                // Otherwise
                else
                {
                    items[i].right = (slot * (items.length - key - 1)) + '%'
                }

                // Break
                break

        }

    }

    // Add the item to the view
    view.add(items[i])

}

これは、場合によっては許容範囲内 (完全ではない) に機能しますが、柔軟なスペースが使用されている場合は、定義上、割り当てた固定幅のスロットに収まらないため、常にそうとは限りません。

どんな助けでも大歓迎です。

編集:

私は動的な解決策を探していることに言及する必要があります。個々のケースで左右の位置を明示的に定義できることはわかっていますが、(可能であれば) オブジェクトの配列を関数に渡して、ボタンを備えた疑似ツールバーとしてオブジェクトをTi.UI.Button返すようにしたいと考えています。Ti.Ui.View柔軟なスペースの存在に応じて配置されます。

4

1 に答える 1

0

OK、いくつか遊んだ後、解決策を思いついたと思います。ツールバーと各項目の幅を明示的に定義する必要がありますが、それはそれほど問題ではありません。ツールバーの幅から各非フレキシブル スペースの幅を差し引き、残りをフレキシブル スペースの数で割ると、各フレキシブル スペースの幅が得られます。次に、各項目が繰り返されてツールバーに追加されます。ただし、フレキシブル スペースは、フレキシブル スペースの幅と同じ幅の空のビューに切り替えられます。以下は実際のコードです。

// Create a toolbar
function createToolbar(data)
{

    // Create the toolbar
    var toolbar = Ti.UI.createView(data)

    // Set the height
    toolbar.height = 44

    // Set the layout
    toolbar.layout = 'horizontal'

    // If there are items
    if (undefined !== toolbar.items)
    {

        // The width
        var width = toolbar.width

        // The number of flexible spaces
        var spaces = 0

        // Iterate over each item
        for (var i in toolbar.items)
        {

            // If not a flexible space then subtract the item width from the total width
            if ('FLEXIBLE_SPACE' !== toolbar.items[i].systemButton)
            {
                width -= toolbar.items[i].width
            }

            // Otherwise increment the number of flexible spaces
            else
            {
                ++spaces
            }

        }

        // Iterate over each item
        for (var i in toolbar.items)
        {

            // If the item is a flexible space
            if ('FLEXIBLE_SPACE' === toolbar.items[i].systemButton)
            {

                // Create the flexible space
                var space = Ti.UI.createView(
                {
                    width: width / spaces
                }
                )

                // Add the space to the toolbar
                toolbar.add(space)

            }

            // Otherwise add the item to the toolbar
            else
            {
                toolbar.add(toolbar.items[i])
            }

        }

    }

    // Return the toolbar
    return toolbar

}
于 2013-01-17T10:33:23.443 に答える