我有一个自定义视图,我把它放在ScrollView中:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollview"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<com.test.view android:id="@+id/myview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</ScrollView>
我在自定义视图中实现了onDraw()和许多其他方法,也是onMeasure:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(600, MeasureSpec.EXACTLY));
width = MeasureSpec.getSize(widthMeasureSpec);
height = MeasureSpec.getSize(heightMeasureSpec);
}
然后当我启动应用程序时,我从onDraw()方法获取了许多日志,并且TouchEvent可以正常处理,但我没有看到我的自定义视图。它似乎在那里,但好像它是看不见的......
也许我错过了什么......?
由于
更新
自定义视图没有什么特别之处,只是一个显示位图的视图:
private class myView extends View{
public myView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.packagebm);
canvas.drawBitmap(bm, 0, 0, null);
Log.i("myview", "onDraw()");
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(300, MeasureSpec.EXACTLY));
}
}
答案 0 :(得分:0)
您必须在自定义视图的onMeasure中调用setMeasuredDimension
。所以看起来应该是这样的:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, height);
setMeasuredDimension(widthMeasureSpec, resolveSize(600, heightMeasureSpec));
}