如何在 Layout Android Xamarin c# 中显示视图



朋友们,我是 xamarin 和 android 开发的新手,我已经创建了画布绘图布局,并且工作正常,但现在我想用按钮在我的 AXML 布局中显示它,有人可以帮助我吗?以下是我的观点:

public class DrawView : View
{
    public DrawView(Context context, IAttributeSet attrs) :
        base(context, attrs)
    {
        Initialize();
    }
    public DrawView(Context context, IAttributeSet attrs, int defStyle) :
        base(context, attrs, defStyle)
    {
        Initialize();
    }
    private void Initialize()
    {
    }
    public DrawView(Context context) : base(context)
    {
    }
    private Path drawPath;
    private Paint drawPaint, canvasPaint;
    private uint paintColor = 0xFF660000;
    private Canvas drawCanvas;
    private Bitmap canvasBitmap;

    public void start()
    {
        drawPath = new Path();
        drawPaint = new Paint();
        drawPaint.Color = new Color((int)paintColor);
        drawPaint.AntiAlias = true;
        drawPaint.StrokeWidth = 20;
        drawPaint.SetStyle(Paint.Style.Stroke);
        drawPaint.StrokeJoin = Paint.Join.Round;
        drawPaint.StrokeCap = Paint.Cap.Round;
        canvasPaint = new Paint();
        canvasPaint.Dither = true;
    }
    protected override void OnSizeChanged(int w, int h, int oldw, int oldh)
    {
        base.OnSizeChanged(w, h, oldw, oldh);
        canvasBitmap = Bitmap.CreateBitmap(w, h, Bitmap.Config.Argb8888);
        drawCanvas = new Canvas(canvasBitmap);
    }
    protected override void OnDraw(Canvas canvas)
    {
        canvas.DrawBitmap(canvasBitmap, 0, 0, canvasPaint);
        canvas.DrawPath(drawPath, drawPaint);
    }

    public override bool OnTouchEvent(MotionEvent e)
    {
        float touchX = e.GetX();
        float touchY = e.GetY();
        switch (e.Action)
        {
            case MotionEventActions.Down:
                drawPath.MoveTo(touchX, touchY);
                break;
            case MotionEventActions.Move:
                drawPath.LineTo(touchX, touchY);
                break;
            case MotionEventActions.Up:
                drawCanvas.DrawPath(drawPath, drawPaint);
                drawPath.Reset();
                break;
            default:
                return false;
        }
        Invalidate();
        return true;
    }

这是我的活动:

[Activity(Label = "SignItActivity")]
public class SignItActivity : Activity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        // Create your application here
        DrawView test = new DrawView(this);
        test.start();
    }
}

现在这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px">
<RelativeLayout
    android:minWidth="25px"
    android:minHeight="25px"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/relativeLayout1">
    <Button
        android:text="Начать заново"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btnClear"
        android:layout_marginRight="10dp"
        android:layout_marginLeft="200dp" />
    <Button
        android:text="Сохранить"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btnSave"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="200dp" />
</RelativeLayout>
<View
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/myview"
    android:background="#fff" />
 </LinearLayout>

现在如何在布局中显示我的绘图视图。多谢。

请在使用之前添加您的软件包名称。

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minWidth="25px"
    android:minHeight="25px">
    <RelativeLayout
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/relativeLayout1">
        <Button
            android:text="Начать заново"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/btnClear"
            android:layout_marginRight="10dp"
            android:layout_marginLeft="200dp" />
        <Button
            android:text="Сохранить"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/btnSave"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="200dp" />
    </RelativeLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/myview"
        android:background="#fff" />
     <package.DrawView 
                    android:id="@+id/DrawView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center_horizontal" />
     </LinearLayout>

最新更新