Quick-cocos2dx容器层的使用
本文转载于:http://www.cocos2d-x.org/docs/manual/framework/native/wiki/containers/zh
GUI 控件我们大致可以分为两类,**普通控件** 和 容器控件,普通控件指的是一些常用的控件,如 UIButton,UILabel,UISlider 和 UITextField 等控件,而容器控件如 UILayout,UIScrollView,UIListView,UIPageView 等,这些容器控件都有一个特点,它可以作为容器,包含其它控件,虽然所有的控件都能够包含其它控件,但有些控件的职责非常单一,如按钮标签等,并不经常向其添加其它控件。以下详细介绍容器控件的使用方法。
UILayout (Panel)
Panel 作为最主要的容器层,前面我们说过,由 CocoStudio UI 编辑器所创建的 UI 是基于 Panel 来布局的,要想熟练的使用 UI 控件,了解 Panel 以及其属性也是重中之重,既然是容器,容器里面总得有些内容了,Panel 对应得控件名称为 UILayout。
Size widgetSize = m_pWidget->getSize();
UILayout *background = dynamic_cast<UILayout*>(m_pUiLayer->getWidgetByName("background_Panel"));
// Create the layout
UILayout* layout = UILayout::create();
layout->setSize(Size(280, 150));
Size backgroundSize = background->getSize();
layout->setPosition(Point((widgetSize.width - backgroundSize.width) / 2 +
(backgroundSize.width - layout->getSize().width) / 2,
(widgetSize.height - backgroundSize.height) / 2 +
(backgroundSize.height - layout->getSize().height) / 2));
m_pUiLayer->addWidget(layout);
UIButton* button = UIButton::create();
button->setTouchEnabled(true);
button->loadTextures("cocosgui/animationbuttonnormal.png", "cocosgui/animationbuttonpressed.png", "");
button->setPosition(Point(button->getSize().width / 2, layout->getSize().height - button->getSize().height / 2));
layout->addChild(button);
UIButton* textButton = UIButton::create();
textButton->setTouchEnabled(true);
textButton->loadTextures("cocosgui/backtotopnormal.png", "cocosgui/backtotoppressed.png", "");
textButton->setTitleText("Text Button");
textButton->setPosition(Point(layout->getSize().width / 2, layout->getSize().height / 2));
layout->addChild(textButton);
UIButton* button_scale9 = UIButton::create();
button_scale9->setTouchEnabled(true);
button_scale9->loadTextures