Xamarin.Android中的矩形

Xamarin.Android中的矩形,xamarin.android,Xamarin.android,如何通过designer(Main.axml)或code或Strings.xml文件在Xamarin.Android(在Visual Studio中)中绘制或放置矩形?有几种方法可以实现这一点 在定义矩形的位置使用可绘制的xml文件 使用表示矩形的9面片图像 使用显示矩形的图像 创建一个自定义的视图,在其中覆盖OnDraw方法并在那里绘制矩形 对于1。您可以执行以下操作: <shape xmlns:android="http://schemas.android.com/apk/r

如何通过designer(Main.axml)或code或Strings.xml文件在Xamarin.Android(在Visual Studio中)中绘制或放置矩形?

有几种方法可以实现这一点

  • 在定义矩形的位置使用可绘制的xml文件
  • 使用表示矩形的9面片图像
  • 使用显示矩形的图像
  • 创建一个自定义的
    视图
    ,在其中覆盖
    OnDraw
    方法并在那里绘制矩形
  • 对于1。您可以执行以下操作:

    <shape 
        xmlns:android="http://schemas.android.com/apk/res/android"  
        android:shape="rectangle">
    
        <solid android:color="#ffffff"/>
    
        <size android:height="20dp" />
    </shape>
    
    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);
            }
        }
    }
    

    好的,非常感谢!如果这对你有帮助,请将其标记为答案