自定义列表框,可以调整高度。
收藏
我是用的自定义列表控件实现的。继承了CAknSingleStyleListBox。
//------------------------------------------------ ----头文件------------------------------------------------
#ifndef __CMySingleStyleListBox__
#define __CMySingleStyleListBox__
#include
#include
class CControl;
class CFbsBitmap;
class CMaxIcon; //是我自定义的图片类。
class CMySingleStyleListBox : public CAknSingleStyleListBox
{
public:
CMySingleStyleListBox(CControl * aControl);
virtual ~CMySingleStyleListBox();
protected:
virtual void CreateItemDrawerL();
CControl * iControl;
RArray iSelectRecordID; //在列表中显示的记录ID
CMaxIcon * iBack; //背景颜色
CMaxIcon * iSelectedBack; //选中的背景
};
class CMyColumnListBoxItemDrawer : public CColumnListBoxItemDrawer
{
public:
CMyColumnListBoxItemDrawer(MTextListBoxModel* aTextListBoxModel,
const CFont* aFont, CColumnListBoxData* aColumnData,
CEikListBox* aListBox, CControl * aControl, CMaxIcon * aBack,
CMaxIcon * aSelBack);
virtual ~CMyColumnListBoxItemDrawer();
virtual void DrawActualItem(TInt aItemIndex, const TRect& aActualItemRect,
TBool aItemIsCurrent, TBool aViewIsEmphasized, TBool,
TBool aItemIsSelected) const;
void SetFindBox(CAknSearchField* aFindBox);
CAknSearchField* iFindBox;
CEikListBox* iOwner;
CControl * iControl;
RArray iSelectRecordID; //在列表中显示的记录ID
MAknsSkinInstance* skin;
//图片
CMaxIcon * iBack; //背景颜色
CMaxIcon * iSelectedBack; //选中的背景
};
#endif
//------------------------------------------------ ----定义文件------------------------------------------------
#include
#include
//
#include "manager/Control.h"
#include "manager/Helper.h"
#include "manager/MaxIcon.h"
#include "control/MySingleStyleListBox.h"
////////////////////////////////////////////////// ////////////////////
// Construction/Destruction
////////////////////////////////////////////////// ////////////////////
#define MEM_FREE(a) if(a){delete a; a=NULL;}
CMySingleStyleListBox::CMySingleStyleListBox(CCont rol * aControl)
{
iControl = aControl;
iBack = NULL;
iSelectedBack = NULL;
}
void CMySingleStyleListBox::CreateItemDrawerL()
{
CColumnListBoxData* columnData = CColumnListBoxData::NewL();
EnableDragEvents();
const CFont* myFont;
myFont = CEikonEnv::Static()->DenseFont();
CEikListBox *aListBox = this;
//加载图片
CMaxIconManager* pIconMgr = iControl->iIconManager;
//背景
iBack = pIconMgr->GetMaxIcon(CMaxIconManager::EListBack);
iSelectedBack = pIconMgr->GetMaxIcon(CMaxIconManager::EListCur);
iItemDrawer = new (ELeave) CMyColumnListBoxItemDrawer(Model(), myFont,
columnData, aListBox, iControl, iBack, iSelectedBack);
//iItemDrawer->SetItemCellSize(TPoint(100,100));
//iItemDrawer->SetItemCellSize(TSize(360,100));
iItemDrawer->SetItemCellSize(TSize(360 , 100));
//View()->SetItemHeight(100) ;
}
CMySingleStyleListBox::~CMySingleStyleListBox()
{
}
// MySingleStyleListBox.cpp: implementation of the CMyColumnListBoxItemDrawer class.
//
////////////////////////////////////////////////// ////////////////////
CMyColumnListBoxItemDrawer::CMyColumnListBoxItemDr awer(
MTextListBoxModel* aTextListBoxModel, const CFont* aFont,
CColumnListBoxData* aColumnData, CEikListBox* aListBox,
CControl * aControl, CMaxIcon * aBack, CMaxIcon * aSelBack) :
CColumnListBoxItemDrawer(aTextListBoxModel, aFont, aColumnData), iOwner(
aListBox)
{
iControl = aControl;
iBack = aBack;
iSelectedBack = aSelBack;
//SetItemCellSize(TSize(360, 150));
}
void CMyColumnListBoxItemDrawer::DrawActualItem(TInt aItemIndex,
const TRect& aActualItemRect, TBool aItemIsCurrent,
TBool aViewIsEmphasized, TBool, TBool aItemIsSelected) const
{
//需要修改 高亮
/*
MAknsSkinInstance* skin = AknsUtils::SkinInstance();
MAknsControlContext* cc = AknsDrawUtils::ControlContext(iOwner);
if (skin && cc)
{
AknsDrawUtils::Background(skin, cc, iOwner, *iGc, aActualItemRect);
}
*/
TRect aRect = aActualItemRect;
aRect.iBr.iX += 30;
TRgb rSelected(TRgb(228, 100, 41)); //选中的颜色
TRgb rUnSelected(TRgb(255, 255, 255)); //没有选中的颜色
TRect outer_rect(aRect);
outer_rect.Shrink(2, 2);
TRect inner_rect(outer_rect);
inner_rect.Shrink(3, 3);
/*
//画背景图片
NHelper::DrawMaxIcon(*iGc, aActualItemRect, iBack);
*/
/*
if (aItemIsCurrent)
{
NHelper::DrawMaxIcon(*iGc, aActualItemRect, iSelectedBack);
}
else
{
NHelper::DrawMaxIcon(*iGc, aActualItemRect, iBack);
}
*/
if (aItemIsCurrent)
{
//NHelper::DrawMaxIcon(*iGc, aActualItemRect, iSelectedBack);
iGc->DrawBitmap(aRect, iSelectedBack->m_pBitmap);
}
else
{
//NHelper::DrawMaxIcon(*iGc, aActualItemRect, iBack);
iGc->DrawBitmap(aRect, iBack->m_pBitmap);
}
//初始黑色
const CFont* font = CEikonEnv::Static()->NormalFont();
iGc->UseFont(font);
iGc->SetPenStyle(CGraphicsContext::ESolidPen);
iGc->SetPenColor(iHighlightedTextColor);
TPtrC itemText = iModel->ItemText(aItemIndex);
TInt baseline = (aRect.Height() - font->HeightInPixels()) / 2
+ font->AscentInPixels();
//计算每段字符串长度
TInt highlightwidth = font->TextWidthInPixels(itemText);
TRect highlighted = aRect;
highlighted.SetWidth(highlightwidth);
//画每段字符串iTextColor
iGc->SetPenColor(rUnSelected);
iGc->SetPenStyle(CGraphicsContext::ESolidPen);
iGc->DrawText(itemText, highlighted, baseline);
//释放字体
iGc->DiscardFont();
}
CMyColumnListBoxItemDrawer::~CMyColumnListBoxItemD rawer()
{
}
//------------------------------------------------ ----注意事项------------------------------------------------
我终于找到解决的办法了。办法如下:
1.在创建列表元素的时候设置一下大小
iListShopBox->View()->SetItemHeight(100);
2.在CreateItemDrawerL()中添加
iItemDrawer->SetItemCellSize(TSize(360 , 100));
就OK了……