cocos2dx控件-----ListView的使用(label自动换行)

来源:互联网 发布:网络借贷陷阱 编辑:程序博客网 时间:2024/06/05 04:30
float totalHeight = 0;
if (m_textInfoListView)
{
m_textInfoListView->removeAllChildrenWithCleanup(true);
}
m_textInfoListView = nullptr;
m_textInfoListView = ListView::create();
m_textInfoListView->setAnchorPoint(Vec2(0.5f,1.0f));//设置listview锚点
auto winSize = Director::getInstance()->getWinSize();
m_textInfoListView->setPosition(Vec2(winSize.width / 2, winSize.height*0.8));//设置ListView的位置
m_textInfoListView->setContentSize(Size(600, 950));//设置listview 的大小
m_bgPannel->addChild(m_textInfoListView);


//MKJsonDoc doc;解析json
if (doc.parseFile("configs/how_to_play_info.json"))
{
auto tipsArray_value = doc.getValue(StringUtils::format("tips_%s", key.c_str()).c_str());
if (tipsArray_value.isArray())
{
for (int i = 0; i < tipsArray_value.size(); ++i)
{
//create label begin
Label* label = Label::create();
label->setAnchorPoint(Vec2(0, 0.5f));
if (i == 0)
{
label->setSystemFontSize(48);
}
else
{
label->setSystemFontSize(36);
}


if ("score" == key)
{
if (i == 7 || i == 10)
{
label->setSystemFontSize(48);
}
}


//label->setLineBreakWithoutSpace(true);
//label->setMaxLineWidth(100);//m_textInfoListView->getContentSize().width - 60
label->setDimensions(m_textInfoListView->getContentSize().width,0);//label自动换行
label->setString(tipsArray_value.getValue(i).getString());
//label end
totalHeight += label->getContentSize().height;
//layout begin
Layout* layout = Layout::create();//把一个layout添加到listview中,layout里面可以添加很多控件实现不同的效果。
layout->setAnchorPoint(Vec2(0, 0));
layout->setContentSize(Size(m_textInfoListView->getContentSize().width, label->getContentSize().height));//设置layout的高度
label->setPosition(Vec2(0, layout->getContentSize().height / 2));
layout->addChild(label, 10);
//layout->setBackGroundImageScale9Enabled(true);
//layout->setBackGroundImage("textures/settings/howToPlay/play_bg.png");
layout->setPosition(Vec2(30, 0));
//layout end
m_textInfoListView->pushBackCustomItem(layout);//往listview中添加数据
}


}
}
原创粉丝点击