Java 视图的DispatchDraw方法不起作用

Java 视图的DispatchDraw方法不起作用,java,c#,android,xamarin,xamarin.android,Java,C#,Android,Xamarin,Xamarin.android,我试图重写View类的DispatchDraw方法以显示某些内容。我的看法如下: public class MyCustomButton : View { private Paint colorPaint; public MyCustomButton(Context context, IAttributeSet attrs) : base(context, attrs) { colorPaint = new Paint(); colorPa

我试图重写View类的DispatchDraw方法以显示某些内容。我的看法如下:

public class MyCustomButton : View
{
    private Paint colorPaint;
    public MyCustomButton(Context context, IAttributeSet attrs) : base(context, attrs)
    {
        colorPaint = new Paint();
        colorPaint.Color = Color.Argb(0, 255, 0, 0);
    }
    protected override void DispatchDraw(Canvas canvas)
    {
        canvas.DrawText("aaa", 0, 0, colorPaint);
    }
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <Hw005_UiControls.MyCustomButton
        android:id="@+id/myBtn"
        android:layout_width="match_parent"
        android:layout_height="64dp"
        android:background="@android:color/holo_blue_bright" />
</RelativeLayout>
...
SetContentView(Resource.Layout.ButtonView5);
View view = FindViewById(Resource.Id.myBtn) as View;
view.Invalidate();
...
活动布局文件(Xamarin中的axml文件)如下:

public class MyCustomButton : View
{
    private Paint colorPaint;
    public MyCustomButton(Context context, IAttributeSet attrs) : base(context, attrs)
    {
        colorPaint = new Paint();
        colorPaint.Color = Color.Argb(0, 255, 0, 0);
    }
    protected override void DispatchDraw(Canvas canvas)
    {
        canvas.DrawText("aaa", 0, 0, colorPaint);
    }
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <Hw005_UiControls.MyCustomButton
        android:id="@+id/myBtn"
        android:layout_width="match_parent"
        android:layout_height="64dp"
        android:background="@android:color/holo_blue_bright" />
</RelativeLayout>
...
SetContentView(Resource.Layout.ButtonView5);
View view = FindViewById(Resource.Id.myBtn) as View;
view.Invalidate();
...
我跟踪这些代码,每行代码都可以执行,但字符串“aaa”可以执行 显示。我使用xamarin,但在本例中,它几乎与java相同。也是如此 有人能给我一些解决这个问题的建议吗


非常感谢

如果您使用的是
视图组
并且需要渲染
视图
子对象,则通常会使用
DispatchDraw
。另外,
DrawText
Y
参数是文本的基线,而不是
Y
原点

使用OnDraw覆盖的示例: 按钮子类上的OnDraw覆盖: