1. Overview
In the previous article we designed a static uiscrollview in the editor, and usually we need to add information dynamically in the program. How is the position of the inserted element determined? In 3.0 Uilayout has implemented the basic layout, take a look at it:
2. Editing interface
Open Cocostudio uieditor Edit an interface to create a ScrollView and two button. Because ScrollView inherits from Uilayout, we use it here. It's not too good. Children's shoes can be consulted: Cocos2d-x 3.0 Development (14) using Uiscrollview to achieve the size of different items drag display
note here that scrollview the child control layout, we select Linear Portrait, and the slide direction with properties is also selected as "vertical". Save the export after editing it well.
3. Related Programs
Run the script, create a testlayout program, and copy the exported files to the resource directory.
Declare a uilayout:
[CPP]View Plaincopyprint?
- #include "gui\cocosgui.h"
- Private
- gui::uilayout* m_layout;
Change init:
[CPP]View Plaincopyprint?
- BOOL Helloworld::init ()
- {
- //////////////////////////////
- //1. Super init First
- if (! Layer::init ())
- {
- return false;
- }
- Size visiblesize = director::getinstance ()->getvisiblesize ();
- Point origin = Director::getinstance ()->getvisibleorigin ();
- //load Layout
- M_layout = dynamic_cast<uilayout*> (Cocostudio::guireader::sharereader ()->widgetfromjsonfile (" Testlayout.json "));
- Auto layer = Uilayer::create ();
- Layer->addwidget (m_layout);
- this->addchild (layer);
- //set button React
- uibutton* Buttona = dynamic_cast<uibutton*> (m_layout->getchildbyname ("Textbuttona"));
- Buttona->addtoucheventlistener (this,toucheventselector (Helloworld::touchbutton));
- uibutton* buttonb = dynamic_cast<uibutton*> (m_layout->getchildbyname ("textbuttonb"));
- Buttonb->addtoucheventlistener (this,toucheventselector (Helloworld::touchbutton));
- return true;
- }
Add a response function
[CPP]View Plaincopyprint?
- void Helloworld::touchbutton (cocos2d::object* obj,toucheventtype type)
- {
- if (type = = touch_event_ended)
- {
- Auto button = dynamic_cast<uibutton*> (obj);
- if (strcmp (Button->getname (),"Textbuttona") = = 0)
- {
- //add element on left
- Addleft ();
- }
- Else
- {
- //add element on right
- Addright ();
- }
- }
- }
Compile and run, you can see the following interface:
4. Adding controls
Implement Addleft () and Addright ():
[CPP]View Plaincopyprint?
- void Helloworld::addleft ()
- {
- Auto Layoutparameter = Uilinearlayoutparameter::create ();
- Layoutparameter->setgravity (Uilineargravity::linear_gravity_left);
- Auto ScrollView = ((uiscrollview*) m_layout->getchildbyname ("ScrollView"));
- uiwidget* widget = Makewords ();
- Widget->setlayoutparameter (Layoutparameter);
- Scrollview->addchild (widgets);
- Scrollview->dolayout ();
- }
- void Helloworld::addright ()
- {
- Auto Layoutparameter = Uilinearlayoutparameter::create ();
- Layoutparameter->setgravity (Uilineargravity::linear_gravity_right);
- Auto ScrollView = ((uiscrollview*) m_layout->getchildbyname ("ScrollView"));
- uiwidget* widget = Makewords ();
- Widget->setlayoutparameter (Layoutparameter);
- Widget->setcolor (color3b (0,255,0));
- Scrollview->addchild (widgets);
- Scrollview->dolayout ();
- }
- gui::uiwidget* Helloworld::makewords ()
- {
- uibutton* button = Uibutton::create ();
- Button->loadtexturenormal ("button.png");
- Button->settitletext ("Hello uilayout \ n I ' m cocos2d-x");
- Button->settitlecolor (color3b (0,0,0));
- return button;
- }
Use the overridden Addchild to add child controls. Controls the display position of the control by adjusting its uilinearlayoutparameter.
One thing to note is that when you add a child control in Uiscrollview, you call the Dolayout function to organize the location of the child controls.
The final compilation runs.
5. Summary
with Uilayout layout, you can easily control the location of dynamically added elements.
Demo Download: http://download.csdn.net/detail/fansongy/6730481
This blog from the Asura road , reproduced please indicate the source, prohibited for commercial use: http://blog.csdn.net/fansongy/article/details/17382049
Internship Small white:: (EXT) Cocos2d-x 3.0 Development (15) Use Uilayout Layout, make dialog interface