容器层的使用(UILayout (Panel))
转:http://www.cocos2d-x.org/docs/manual/framework/native/gui/container/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("cocosgui/button.png", "cocosgui/buttonHighlighted.png", ""); button_scale9->setScale9Enabled(true); button_scale9->setSize(Size(100, button_scale9->getContentSize().height)); button_scale9->setPosition(Point(layout->getSize().width - button_scale9->getSize().width / 2, button_scale9->getSize().height / 2)); layout->addChild(button_scale9);
如上面代码所示,我们创建了一个 layout
控件,然后在其中添加了三个控件。m_pUiLayer
是当前场景的一个 UILayer ,前面我们介绍过,所有的 UI 控件,都是放在 UILayer 里面,UILayer 管理所有的控件,并添加到当前场景中去。显示效果如下:
我们设置了 layout 的 size 属性,也就是给它一个大小,但是并没有显示出来效果,默认是透明的,我们可以为这个层设置颜色:
layout->setBackGroundColorType(LAYOUT_COLOR_SOLID); layout->setBackGroundColor(Color3B(128, 128, 128));
当然,除了设置颜色之外,还可以设置我们想要的背景图片:
layout->setSize(Size(280, 150)); layout->setClippingEnabled(true); layout->setBackGroundImage("cocosgui/Hello.png");
如上图显示,我们设置了 size 并且设置了 背景图片,但是不要忘了调用 setClippingEnabled
方法根据 size 进行裁剪,如果忘了调用,那么会向下面所显示的一样。
除了以上使用方式,还有其它玩法:
layout->setBackGroundImageScale9Enabled(true); layout->setBackGroundImage("cocosgui/green_edit.png");
使用九宫格图片做为背景,注意启用此功能。
UILayout 显示颜色的模式有三种
LayoutBackGroundColorType | 说明 |
---|---|
LAYOUT_COLOR_NONE | 透明,没有颜色显示 |
LAYOUT_COLOR_SOLID | 实体,可以设置颜色 |
LAYOUT_COLOR_GRADIENT | 渐变颜色 |
UIPanel 控件的布局方案
UILayout 是作为布局之用,以上都只是修改背景图片,下面除了手动摆放坐标位置的绝对定位,还提供了哪些布局方案呢。
LayoutType | 说明 |
---|---|
LAYOUT_ABSOLUTE | 绝对布局 |
LAYOUT_LINEAR_VERTICAL | 垂直平铺 |
LAYOUT_LINEAR_HORIZONTAL | 横向平铺 |
LAYOUT_RELATIVE | 相对布局 |
layout->setLayoutType(LAYOUT_LINEAR_VERTICAL); // 或者 layout->setLayoutType(LAYOUT_LINEAR_HORIZONTAL); // 或者 layout->setLayoutType(LAYOUT_RELATIVE);
注意:除了绝对定位之外,如果设置了其它布局方案,那么 UIPanel 会忽略其内部控件本身设置的位置。而此时可以使用提供的UILayoutParameter
来设置位置关系,根据布局方案提供了几种布局参数, UILinearLayoutParameter
和UIRelativeLayoutParameter
。下面介绍如何实用布局参数来配合布局设计界面显示效果。
layout->setLayoutType(LAYOUT_LINEAR_VERTICAL); // .... 省略控件创建代码,同前文控件一样 UILinearLayoutParameter* lp1 = UILinearLayoutParameter::create(); lp1->setGravity(LINEAR_GRAVITY_CENTER_HORIZONTAL); lp1->setMargin(UIMargin(0, 10, 0, 10)); UILinearLayoutParameter* lp2 = UILinearLayoutParameter::create(); lp2->setGravity(LINEAR_GRAVITY_CENTER_HORIZONTAL); lp2->setMargin(UIMargin(20, 20, 0, 5)); UILinearLayoutParameter* lp3 = UILinearLayoutParameter::create(); lp3->setGravity(LINEAR_GRAVITY_CENTER_HORIZONTAL); lp3->setMargin(UIMargin(0, 10, 0, 10)); button->setLayoutParameter(lp1); textButton->setLayoutParameter(lp2); button_scale9->setLayoutParameter(lp3);
显示效果如下:
我们看到,分别创建了三个布局参数 UILinearLayoutParameter
,设置了 Gravity
和 Margin
参数,然后给三个 UIPanel 的内部控件分别设置其布局参数值,已达到如上效果。
这里使用的方案是垂直平铺,而每个布局参数设置的 Gravity
值为 LINEAR_GRAVITY_CENTER_HORIZONTAL
,也就是说水平剧中显示,而 Margin 则标示控件四周边缘的间距,注意以上的 lp2
的值为 UIMargin(20, 20, 0, 5)
,其代表含义,距离左、上、右、下的间距。左值为 20,可以看见 textButton
相对中间位置向右便宜少数。这是垂直布局,而水平布局除了方向不一,基本使用方式同垂直布局同样。两者也都叫 线性布局,使用同样的线性布局参数。下面再看看相对布局:
layout->setLayoutType(LAYOUT_RELATIVE); // 此处省略控件的创建步骤 ... UIRelativeLayoutParameter* rp1 = UIRelativeLayoutParameter::create(); rp1->setAlign(RELATIVE_ALIGN_PARENT_TOP_RIGHT); UIRelativeLayoutParameter* rp2 = UIRelativeLayoutParameter::create(); rp2->setAlign(RELATIVE_ALIGN_PARENT_LEFT_CENTER_VERTICAL); UIRelativeLayoutParameter* rp3 = UIRelativeLayoutParameter::create(); rp3->setAlign(RELATIVE_ALIGN_PARENT_RIGHT_BOTTOM); button->setLayoutParameter(rp1); textButton->setLayoutParameter(rp2); button_scale9->setLayoutParameter(rp3);
这里创建了三个布局属性,设置了不同的 "停靠" 参数 Align
。