更改Titanium活动选项卡突出显示颜色

时间:2011-04-22 20:10:09

标签: javascript iphone mobile titanium

如何更改Titanium中选定/活动的标签的蓝色突出显示颜色?是否可以为选定的选项卡定义活动状态图标?

2 个答案:

答案 0 :(得分:2)

理想的解决方案是使用:

Ti.UI.createTab({
    icon: '/images/inactive_icon.png',
    activeIcon: '/images/active_icon.png',
});

需要Ti SDK 3.1.0

答案 1 :(得分:1)

一种选择是向选项卡组本身添加元素。然后,当标签获得焦点时,您可以在其上方显示图像。

这是必要的,因为(如上面评论中所述),iOS不允许我们自定义标签的颜色或突出显示样式。

以下是如何实现这一目标的示例。如果你想要的只是所选择的图像不同,你可以做一些不太复杂的事情。

希望这有帮助! -Dawson

/**
 * Override a tab group's tab bar on iOS.
 *
 * NOTE: Call this function on a tabGroup AFTER you have added all of your tabs to it! We'll look at the tabs that exist
 * to generate the overriding tab bar view. If your tabs change, call this function again and we'll update the display.
 *
 * @param tabGroup The tab group to override
 * @param backgroundOptions The options for the background view; use properties like backgroundColor, or backgroundImage.
 * @param selectedOptions The options for a selected tab button.
 * @param deselectedOptions The options for a deselected tab button.
 */
function overrideTabs(tabGroup, backgroundOptions, selectedOptions, deselectedOptions) {

    // are we restyling the tab groups?
    if (tabGroup.overrideTabs) {
        tabGroup.remove(tabGroup.overrideTabs);
    }

    // a bunch of our options need to default to 0 for everything to position correctly; we'll do it en mass:
    deselectedOptions.top = deselectedOptions.bottom = selectedOptions.top = selectedOptions.bottom = backgroundOptions.left
            = backgroundOptions.right = backgroundOptions.bottom = 0;

    // create the overriding tab bar using the passed in background options
    backgroundOptions.height = 50;
    var background = Ti.UI.createView(backgroundOptions);

    // and create our individual tab buttons
    var activeTab = null, increment = 100 / tabGroup.tabs.length;
    deselectedOptions.width = selectedOptions.width = String(increment) + '%';
    for (var i = 0, l = tabGroup.tabs.length; i < l; i++) {
        (function(i) {
            var tab = tabGroup.tabs[i];
            selectedOptions.left = deselectedOptions.left = String(increment * i) + '%';

            // set the title of the button to be the title of the tab
            selectedOptions.title = deselectedOptions.title = tab.title;

            tab.selected = Ti.UI.createButton(selectedOptions);
            tab.deselected = Ti.UI.createButton(deselectedOptions);
            tab.deselected.addEventListener('click', function() {
                if (activeTab) {
                    activeTab.selected.visible = false;
                }
                tab.selected.visible = true;
                activeTab = tab;
                tabGroup.setActiveTab(i);
            });
            tab.selected.visible = false;
            background.add(tab.deselected);
            background.add(tab.selected);
        })(i);
    }

    tabGroup.add(background);
    tabGroup.overrideTabs = background;

    // "click" the first tab to get things started
    tabGroup.tabs[0].deselected.fireEvent('click');
}

/*
 The rest is a typical new project -- a tab group with three tabs.
 */

var tabGroup = Ti.UI.createTabGroup();
tabGroup.addTab(Ti.UI.createTab({
    title: 'Tab 1',
    window: Ti.UI.createWindow({ title: 'Tab 1', backgroundColor: '#fff' })
}));
tabGroup.addTab(Ti.UI.createTab({
    title: 'Tab 2',
    window: Ti.UI.createWindow({ title: 'Tab 2', backgroundColor: '#fff' })
}));
tabGroup.addTab(Ti.UI.createTab({
    title: 'Tab 3',
    window: Ti.UI.createWindow({ title: 'Tab 3', backgroundColor: '#fff' })
}));

/*
 Now call our overrideTabs function!
 */
overrideTabs(
    tabGroup,
    { backgroundColor: '#f00' },
    { backgroundColor: '#999', color: '#000', style: 0 },
    { backgroundColor: '#333', color: '#888', style: 0 }
);

tabGroup.open();

(取自我原来的Gist:https://gist.github.com/853935