在Cocos2dx最新版本中(cocos2dx3.13)中,增加了控件TabControl
例子:
auto tab = ui::TabControl::create();
tab->setContentSize(Size(400.f,400.f));
tab->setHeaderHeight(100.f);
tab->setHeaderWidth(200.f);
tab->setHeaderSelectedZoom(.1f);
tab->setHeaderDockPlace(ui::TabControl::Dock::TOP);
auto header1 = ui::TabHeader::create("cross1","check_Box_normal_disable.png","check_Box_active.png");
header1->setTitleText("background");
auto header2 = ui::TabHeader::create("cross2","check_Box_active.png");
auto header3 = ui::TabHeader::create("cross3","check_Box_active.png");
auto container1 = ui::Layout::create();
container1->setOpacity(255);
container1->setBackGroundColorType(ui::Layout::BackGroundColorType::SOLID);
container1->setBackGroundColor(Color3B::GRAY);
container1->setBackGroundColorOpacity(255);
auto container2 = ui::Layout::create();
container2->setBackGroundColorType(ui::Layout::BackGroundColorType::SOLID);
container2->setOpacity(255);
container2->setBackGroundColor(Color3B::BLUE);
container2->setBackGroundColorOpacity(255);
auto container3 = ui::Layout::create();
container3->setBackGroundColorType(ui::Layout::BackGroundColorType::SOLID);
container3->setOpacity(255);
container3->setBackGroundColor(Color3B::RED);
container3->setBackGroundColorOpacity(255);
tab->insertTab(0,header1,container1);
tab->insertTab(1,header2,container2);
tab->insertTab(2,header3,container3);
tab->setSelectTab(2);
addChild(tab);
但这里有个问题,如果TabControl加入到包含屏蔽Touch的层中,TabHeader无法点击。 其原因是在函数insertTab中,TabHeader是使用 addProtectedChild(header,-2,-1); 加入到TabControl中的,如果改为addChild就正常了,其深层原因我还没有仔细研究!