duilib 之 动态添加BOX布局

杀马特。学长 韩版系。学妹 提交于 2020-02-17 19:05:37

 

 

动态添加BOX布局

Box* GlobalManager::CreateBoxWithCache(const std::wstring& strXmlPath, CreateControlCallback callback)
{
	Box* box = nullptr;
	auto it = m_builderMap.find(strXmlPath);
	if (it == m_builderMap.end()) {
		WindowBuilder* builder = new WindowBuilder();
		box = builder->Create(strXmlPath.c_str(), callback);
		if (box) {
			m_builderMap[strXmlPath].reset(builder);
		}
		else {
			ASSERT(FALSE);
		}
	}
	else {
		box = it->second->Create(callback);
	}

	return box;
}

 

 


void GlobalManager::FillBoxWithCache(Box* pUserDefinedBox, const std::wstring& strXmlPath, CreateControlCallback callback)
{
	Box* box = nullptr;
	auto it = m_builderMap.find(strXmlPath);
	if (it == m_builderMap.end()) {
		WindowBuilder* winBuilder = new WindowBuilder();
		box = winBuilder->Create(strXmlPath.c_str(), callback, pUserDefinedBox->GetWindow(), nullptr, pUserDefinedBox);
		if (box) {
			m_builderMap[strXmlPath].reset(winBuilder);
		}
		else {
			ASSERT(FALSE);
		}
	}
	else {
		box = it->second->Create(callback, pUserDefinedBox->GetWindow(), nullptr, pUserDefinedBox);
	}

	return;
}

 

 

1  列表项中,动态添加一布局项

   

 

 

   列表子项XML布局

<?xml version="1.0" encoding="utf-8"?>
<Window>
    <ListContainerElement class="xnw_listitem" width="stretch" height="180" bkcolor="xnw_client"  >
      
        <VBox>
            <HBox name="storePushUrl" margin="0,0,0,0" childmargin="12" height="18">
                 <Control name="statusImg" width="auto" height="auto"/>
                 <Label name="live_status" text="正在直播..." font="7" normaltextcolor="xnw_yellow_text" />
                 <Label  text="9:00--11:00" font="1" normaltextcolor="xnw_time_text" name="time" />        
            </HBox>

        </VBox>
    </ListContainerElement>
</Window>

 

使用 CreateBoxWithCache  或 FillBoxWithCache

 

示例:

 

	ui::ListContainerElement* pItem = GetItemAt(0);
	ui::ListContainerElement* pNewItem = NULL;

	for (int nIndex = 1; nIndex < nCount; nIndex++){
		pNewItem = new ui::ListContainerElement;
		ui::GlobalManager::FillBoxWithCache(pNewItem, L"myClass/myclass_list_item.xml");
	
		std::string itemName = MyString::n2str(nIndex);
		pNewItem->SetUTF8Name(itemName); //设置item标识名字

		SetItemDetailedInfo(pNewItem);
		myClass_list->Add(pNewItem);
	}

 

AddListItem(int nIndex){

	ui::ListContainerElement* pItem = dynamic_cast<ui::ListContainerElement*>(ui::GlobalManager::CreateBoxWithCache(L"myClass/myclass_list_item.xml"));
	myClass_list->Add(pItem);

	std::string itemName = MyString::n2str(nIndex);
	
	pItem->SetUTF8Name(itemName); //设置item标识名字

	SetItemDetailedInfo(pItem);


}

 

		//添加列表中
				ui::ListContainerElement* pItem = dynamic_cast<ui::ListContainerElement*>(ui::GlobalManager::CreateBoxWithCache(L"myClass/myclass_list_item.xml"));
				myClass_list->Add(pItem);

 

示例2 添加HBox布局

 

<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<Window>
	<HBox width="175" height="auto" padding="10,6,10,6" margin="0,0,10,10" bkimage="file='selected_user_bk.png' corner='2,2,2,2'">
		<Label name="show_name" width="stretch" valign="center" font="1" normaltextcolor="dark2color" mouse="false"/>
		<Button class="btn_wnd_close" name="delete" valign="center" margin="8"/>
	</HBox>
</Window>

 

ui::HBox* InvokeChatForm::CreateSelectedListItem(const std::string& accid)
{
	ui::HBox* container_element = (ui::HBox*)GlobalManager::CreateBoxWithCache(L"invokechat/user_photo.xml");
	container_element->SetUTF8Name(accid);
	container_element->SetUTF8DataID(accid);
	Label* show_name_label = (Label*)container_element->FindSubControl(L"show_name");
	show_name_label->SetText(UserService::GetInstance()->GetUserName(accid));
	Button* btn_delete = (Button*)container_element->FindSubControl(L"delete");
	btn_delete->AttachClick(nbase::Bind(&InvokeChatForm::OnBtnDeleteClick, this, accid, std::placeholders::_1));

	return container_element;
}


		ui::HBox* selected_listitem = CreateSelectedListItem(id);
		selected_user_list_->Add(selected_listitem);
		selected_user_list_->EndDown();

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!