如何通过设计器(Xamarin.Android)(Main.axml)或代码或Strings.xml文件在Visual中绘制或放置矩形?
发布于 2013-09-06 09:28:31
有几种方法可以解决这个问题。
Drawable xml文件。View,在其中覆盖OnDraw方法并在那里绘制矩形。对于1.,您可以执行以下操作:
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#ffffff"/>
<size android:height="20dp" />
</shape>然后在布局文件中定义它时,将其用于View中的背景。
对于最后一种方法,您可以这样做:
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);
}
}
}https://stackoverflow.com/questions/18653575
复制相似问题