<当你在xml布局中,已经设置了大小参数的时候,如果需要canvas画图工具实现显示效果,此时,需要用onMeasure()方法得到java代码中需要的宽高,然后进行绘制,有一个地方我不太明白,就是很多博客上都说需要调用setMeasuredDimension()方法,但是我没有用到这个,我在onLayout()里面设置了位置和大小。附上代码:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// 得到mHeaderViewz的宽、高
if (mHeaderView != null) {
//设置mHeaderView的宽度和高度
measureChild(mHeaderView, widthMeasureSpec, heightMeasureSpec);
mHeaderViewWidth = mHeaderView.getMeasuredWidth();
mHeaderViewHeight = mHeaderView.getMeasuredHeight();
}
}
//onLayout()方法的作用是继承自View,当布局发生改变时调用
@Override
protected void onLayout(boolean changed, int left, int top, int right,
int bottom) {
super.onLayout(changed, left, top, right, bottom);
if (mHeaderView != null) {
mHeaderView.layout(0, 0, mHeaderViewWidth, mHeaderViewHeight);
configureHeaderView(getFirstVisiblePosition());
//getFirstVisiblePosition()返回显示在屏幕上的第一个元素在适配器中所处的位置。
}
}
另外附上别人的一些研究和分析:
http://blog.youkuaiyun.com/az44yao/article/details/8272215
第一次写博客,希望能坚持下来