android - 将 View 添加到不带 XML 的 View

标签 android xamarin xamarin.android

我有这个扩展 View 的类(它在 C# 中,因为我使用 Xamarin)

public class ScannerOverlay : View
{

    public ScannerOverlay(Context context) : base(context)
    {
       //Initializing some values here
    }

    protected override void OnDraw(Canvas canvas)
    {
        base.OnDraw(canvas);

        //Draw some stuff on the canvas
    }
}

我使用以下方法创建此 View :View v = new ScannerOverlay(context);

现在我想给这个 View 添加一个按钮。我该怎么做呢?该 View 没有任何布局,因此 AddView() 将不起作用。根据我的发现,不可能在 Canvas 上绘制按钮。

最佳答案

通过从 RelativeLayout 而不是 View 扩展来修复它。

如果(如我的情况)未调用 OnDraw 方法,请在构造函数中设置以下内容:this.SetWillNotDraw(false);

关于android - 将 View 添加到不带 XML 的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56458543/

相关文章:

android - Lombok val 和 var 不适用于 Android

java - 单击“确定”时 EditTexts 始终为空

xaml - 所有标签的自定义字体 Xamarin Forms

xamarin.ios - 使用 xamarin 的 Azure 移动服务 Blob 存储

android - 如何以编程方式设置 TextAppearance

c# - Xamarin C# 无法将 c# 类转换为 Android.content.context

android - 如何预先实例化我的内容提供者?

java - 在相对布局中显示 AChartEngine

mvvm - Xamarin: MVVM交叉信息

android - Mvvmcross 加载指示器未显示