我创建了一个自定义视图-MyImageView-在其中绘制位图,我使用 setLayoutParams() 来设置我的视图的宽度和高度,但它不起作用,我使用日志来跟踪我的视图的宽度和高度,我发现两者其中是0,为什么宽度和高度不是300?这是我的主要活动代码的一部分:
myCanvas=new MyImageView(CanvasTest3Activity.this,Path);
LinearLayout.LayoutParams p=new LinearLayout.LayoutParams(300,300);
myCanvas.setLayoutParams(p);
这是我的 MyImageView 代码的一部分:
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int parentwidth=MeasureSpec.getSize(widthMeasureSpec);
int parentHeight=MeasureSpec.getSize(heightMeasureSpec);
int mywidth=(int)(parentHeight*0.5);
setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec),mywidth);
}