Reputation: 21
How can I draw or put a Rectangle in Xamarin.Android(in Visual Studio) by designer(Main.axml) or from code or Strings.xml file ?
Upvotes: 0
Views: 5761
Reputation: 24460
There are several ways to approach this.
Drawable
xml file where you define a rectangle.View
where you override the OnDraw
method and draw the rectangle there.For 1. you can do something like:
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#ffffff"/>
<size android:height="20dp" />
</shape>
and then use it for the background in a View
when you define it in an layout file.
For the last approach you can do something like:
using Android.App;
using Android.Content;
using Android.Graphics;
using Android.Views;
using Android.OS;
namespace AndroidApplication2
{
[Activity(Label = "AndroidApplication2", MainLauncher = true, Icon = "@drawable/icon")]
public class Activity1 : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
var rectView = new RectangleView(this);
SetContentView(rectView);
}
}
public class RectangleView : View
{
public RectangleView(Context context)
: base(context) { }
protected override void OnDraw(Canvas canvas)
{
var paint = new Paint {Color = Color.Blue, StrokeWidth = 3};
canvas.DrawRect(30, 30, 80, 80, paint);
}
}
}
Upvotes: 2