我是Xamarin monoandroid的新手,我在向我的布局添加自定义视图时遇到了问题。我不知道我做错了什么。很少的帮助将是完整的,这是我的自定义视图类
onDraw方法被调用,但我没有得到输出,也没有错误。
class PositionDrawer : View
{
private int count;
private int position;
private IPositionDrawer cinterface;
private Activity cactivity;
private int container;
private int p;
private ScoreFeed scoreFeed1;
private ScoreFeed scoreFeed2;
public PositionDrawer(Activity context, int count, int positionr, Activity cactivity) :
base (context)
{
//Initialize ();
this.cactivity = cactivity;
this.cinterface = cinterface;
this.count = count;
this.position = positionr;
//this.OnDraw();
Initialize();
}
private void Initialize()
{
//this.Invalidate();
// this.SetMeasuredDimension(100, 100);
this.SetWillNotDraw(false);
Console.WriteLine(this.WillNotDraw());
}
protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
base.OnMeasure(widthMeasureSpec, heightMeasureSpec);
}
public void SetDrawer(int count, int positionr, Activity cactivity)
{
this.cactivity = cactivity;
this.cinterface = cinterface;
this.count = count;
this.position = positionr;
//base(cactivity);
}
protected override void OnDraw(Canvas canvas)
{
base.OnDraw(canvas);
Paint blue = new Paint();
blue.Color = Color.Red;
float screenwidth = cactivity.WindowManager.DefaultDisplay.Width;
canvas.DrawRect(screenwidth * position-1, 0, screenwidth * position, 20, blue);
Console.WriteLine("SW1:" + screenwidth + "pos1:" + position);
// LinearLayout can =cactivity.FindViewById<LinearLayout>(container);
//cinterface.getview(this);
}
}
我使用以下代码将视图添加到布局中。
View pd = new PositionDrawer(this, 3, position, this);
container = FindViewById<LinearLayout>(Resource.Id.position);
container.RemoveAllViews();
container.AddView(pd);
提前谢谢。
答案 0 :(得分:1)
您需要实施正确的ctors。
当您实施View
课程时,您必须至少拥有具有以下签名的ctor:
public PositionDrawer(IntPtr javaReference, JniHandleOwnership transfer)
: base(javaReference, transfer) { }
如果您在View
布局中使用AXML
,则还需要具有以下签名的那个:
public PositionDrawer(Context context, IAttributeSet attrs, int defStyle)
: base(context, attrs, defStyle)
否则,当您从代码创建View
时,您需要向他们提供一些LayoutParameters
,否则他们不知道自己的大小。