2017-01-09 78 views
0

好日子的朋友,我是新的Xamarin和Android開發,我已經創建了畫布繪製佈局,它工作正常,但現在我想要顯示它在我的AXML佈局有按鈕,任何人都可以幫助我嗎? 這是我的看法:如何在佈局中顯示視圖Android Xamarin c#

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> 

現在,我怎麼能在顯示我的drawView函數我佈局。 非常感謝。

+1

您可以在xml類中添加視圖 –

回答

1

在使用之前添加您的包名稱。

<?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>