在学习android Launcher时候,有一个celllayout主要是容纳一个个应用图标的,感觉挺不错的,但是因为功能很多本身实现比较复杂,我简单的实现一下它的栅格功能
首先必须是继承ViewGroup类,这个没有什么好说的
public class CellLayout extends ViewGroup
最主要的要复写OnLayout方法和OnMeasure方法,来测量和布局整个界面
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
//记录屏幕的宽高
mScreenWidth = widthSpecSize;
mScreenHeight = heightSpecSize;
//计算两个cell之间的空隙
mLeftPadding = (mScreenWidth-XCELLCOUNT*CELLWIDTH)/(XCELLCOUNT-1)/2;
mRightPadding = mLeftPadding;