【发布时间】:2011-12-17 13:30:34
【问题描述】:
我创建了一个自定义视图-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);
}
【问题讨论】:
标签: android custom-view layoutparams