Duilib实现自定义动态创建多个按钮
<VerticalLayout name="image_classification" visible="false" > <VerticalLayout name="main_layout" autochangeparentheight="true" autocalcheight="true" autovscroll="true" vscrollbar="true"> <!-- 需要在这里显示按钮 --> </VerticalLayout> </VerticalLayout>
哪里需要用,就调用下面的函数
//自动创建n个按钮 void OnTabImageClick(int n) { CVerticalLayoutUI* pImageLayout = static_cast<CVerticalLayoutUI*>(m_pm.FindControl(_T("image_classification"))); if (!pImageLayout) return; CVerticalLayoutUI* pMainLayout = static_cast<CVerticalLayoutUI*>(m_pm.FindControl(_T("main_layout"))); if (!pMainLayout) return; pMainLayout->RemoveAll(); int buttonsPerRow = 4; int buttonSize = 80; int buttonMargin = 25; int sideMargin = 12; int topMargin = 15; CHorizontalLayoutUI* pRowLayout = nullptr; for (int i = 0; i < n; ++i) { if (i % buttonsPerRow == 0) { pRowLayout = new CHorizontalLayoutUI(); pRowLayout->SetPadding(CDuiRect(sideMargin, topMargin, sideMargin, 0)); pRowLayout->SetFixedHeight(buttonSize + buttonMargin); pMainLayout->Add(pRowLayout); } CButtonUI* pButton = new CButtonUI(); CDuiString strName; strName.Format(_T("btn_square_%d"), i + 1); pButton->SetName(strName); pButton->SetFixedHeight(buttonSize); pButton->SetFixedWidth(buttonSize); pButton->SetBkImage(_T("file='drawable/dumpreport/test.png'")); pButton->SetPadding(CDuiRect(buttonMargin / 2, 0, buttonMargin / 2, 0));
if (pRowLayout) { pRowLayout->Add(pButton); } } pImageLayout->NeedUpdate(); }
我写的n=5,样式:


浙公网安备 33010602011771号