解析:继承ViewGroup后的子类如何重写onMeasure方法
程序员文章站
2023-10-19 14:42:35
1.首先贴上我试验成功的代码复制代码 代码如下:protected void onmeasure(int widthmeasurespec, int heightmeasu...
1.首先贴上我试验成功的代码
protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {
int measurewidth = measurespec.getsize(widthmeasurespec);
int measureheigth = measurespec.getsize(heightmeasurespec);
setmeasureddimension(measurewidth, measureheigth);
// todo auto-generated method stub
for(int i= 0;i<getchildcount();i++){
view v = getchildat(i);
log.v(tag, "measurewidth is " +v.getmeasuredwidth() + "measureheight is "+v.getmeasuredheight());
int widthspec = 0;
int heightspec = 0;
layoutparams params = v.getlayoutparams();
if(params.width > 0){
widthspec = measurespec.makemeasurespec(params.width, measurespec.exactly);
}else if (params.width == -1) {
widthspec = measurespec.makemeasurespec(measurewidth, measurespec.exactly);
} else if (params.width == -2) {
widthspec = measurespec.makemeasurespec(measurewidth, measurespec.at_most);
}
if(params.height > 0){
heightspec = measurespec.makemeasurespec(params.height, measurespec.exactly);
}else if (params.height == -1) {
heightspec = measurespec.makemeasurespec(measureheigth, measurespec.exactly);
} else if (params.height == -2) {
heightspec = measurespec.makemeasurespec(measurewidth, measurespec.at_most);
}
v.measure(widthspec, heightspec);
}
}
解释一下:
首先判断params.width的值是多少,有三种情况。
如果是大于零的话,及传递的就是一个具体的值,那么,构造measuprespec的时候可以直接用exactly。
如果为-1的话,就是matchparent的情况,那么,获得父view的宽度,再用exactly来构造measurespec。
如果为-2的话,就是wrapcontent的情况,那么,构造measurespec的话直接用一个负数就可以了。
复制代码 代码如下:
protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {
int measurewidth = measurespec.getsize(widthmeasurespec);
int measureheigth = measurespec.getsize(heightmeasurespec);
setmeasureddimension(measurewidth, measureheigth);
// todo auto-generated method stub
for(int i= 0;i<getchildcount();i++){
view v = getchildat(i);
log.v(tag, "measurewidth is " +v.getmeasuredwidth() + "measureheight is "+v.getmeasuredheight());
int widthspec = 0;
int heightspec = 0;
layoutparams params = v.getlayoutparams();
if(params.width > 0){
widthspec = measurespec.makemeasurespec(params.width, measurespec.exactly);
}else if (params.width == -1) {
widthspec = measurespec.makemeasurespec(measurewidth, measurespec.exactly);
} else if (params.width == -2) {
widthspec = measurespec.makemeasurespec(measurewidth, measurespec.at_most);
}
if(params.height > 0){
heightspec = measurespec.makemeasurespec(params.height, measurespec.exactly);
}else if (params.height == -1) {
heightspec = measurespec.makemeasurespec(measureheigth, measurespec.exactly);
} else if (params.height == -2) {
heightspec = measurespec.makemeasurespec(measurewidth, measurespec.at_most);
}
v.measure(widthspec, heightspec);
}
}
解释一下:
首先判断params.width的值是多少,有三种情况。
如果是大于零的话,及传递的就是一个具体的值,那么,构造measuprespec的时候可以直接用exactly。
如果为-1的话,就是matchparent的情况,那么,获得父view的宽度,再用exactly来构造measurespec。
如果为-2的话,就是wrapcontent的情况,那么,构造measurespec的话直接用一个负数就可以了。
下一篇: Html学习一