2014-02-19 79 views
0

我有一个custom view,我用它来捕获Android上的用户签名。 view工作正常,我得到我想要的结果。现在我需要添加水印(签名框的背景四角上的小文本)。我在Android和iOS上这样做,所以我在iOS上做的是创建label,并使用一些配置我在运行时计算frame (x,y,width,heigh)并将它们添加到自定义视图。这适用于iOS(MonoTouch)。现在我需要在MonoForAndroid上做同样的事情。将标签和图像动态添加到Android中的自定义视图中

到目前为止,我得到这个:

 
// my customView 
public signatureView : View, ISignatureView 
{ 
    // some irrelvant code here 

    // then OnDraw (which is where I draw the signature line) 
    protected override void OnDraw(Canvas canvas) 
    { 
     DrawWaterMarks(); 
    } 

    private void DrawWaterMarks() 
    { 
     // First, I create a RelativeLayout and add it to my customView to hold the labels 
     _relativeLayout = new RelativeLayout(this.Context); 
     var layoutParam = new RelativeLayout.LayoutParams(this.MeasuredWidth, this.MeasuredHeight); 
     _relativeLayout.LayoutParameters = layoutParam; 
     var viewGroup = (ViewGroup)this.RootView; 
     viewGroup.AddView(_relativeLayout); 

     // I then create the labels 
     ILabel label = new Label(Context); 
     label.Layout(watermark.x, watermark.y, 0,0); 
     EnsureAddingWatermarkControl(label); 

    } 

    private void EnsureAddingWatermarkControl(View view) 
    { 
     if (_relativeLayout != null && view != null) 
     { 
      _relativeLayout.RemoveView(view); 
      _relativeLayout.AddView(view, view.MeasuredWidth, view.MeasuredHeight); 
      this.Invalidate(); 
     }   
    } 

} 

现在上面所有的代码工作正常,没有异常或错误,但我看不到我的任何标签。

我假设它是RelativeLayout以及尺寸的设置和我正在做的方式,但无法解决问题出在哪里。

任何帮助将不胜感激。

回答

0

我解决了这个问题,通过传递我的RelativeLayout的引用来保存SignatureView并使用它保存新(动态)创建的标签(TextView)和水印。这很好。在每个relativeLayout.Add(view)之前,我做了relativeLayout.Remove(view)以确保我没有add相同的view两倍于布局(这可能会抛出exception)。我很快就会写一篇关于此的博客文章,并将在此更新我的答案,以包含该内容

0

这里的问题可能是,它显示已添加到onDraw中的相对布局。因此,在rootview之后或者这种方法

EnsureAddingWatermarkControl(View view) 

你给下面的代码中的注释里添加您的相对布局。

//首先,我创建了一个RelativeLayout的,并将其添加到我的customView到 持有

首先,你应该添加label s到您的相对布局的标签,那么你应该添加您的相对布局您customView(如由您提供的代码段中提到)

使你的代码应该像

private void DrawWaterMarks() 
{ 
     ILabel label = new Label(Context); 
     label.Layout(watermark.x, watermark.y, 0,0); 
     EnsureAddingWatermarkControl(label); 

} 
private void EnsureAddingWatermarkControl(View view) 
{ 
     if (view != null) 
     { 
      _relativeLayout = new RelativeLayout(this.Context); 
     var layoutParam = new RelativeLayout.LayoutParams(this.MeasuredWidth, this.MeasuredHeight); 
     _relativeLayout.LayoutParameters = layoutParam; 
        _relativeLayout.AddView(view, view.MeasuredWidth, view.MeasuredHeight); 

     var viewGroup = (ViewGroup)this.RootView; 
     viewGroup.AddView(_relativeLayout); 


      this.Invalidate(); 
     }   
} 
+0

是否需要再次添加它?我以为我需要添加一次..我会给这个尝试 –

+0

Ur改变相对布局不反映bcoz,你的rootview不知道它的修改。 – Priya

+0

var viewGroup =(ViewGroup)this.RootView; 你能解释一下你为什么使用这条线吗? – Priya

相关问题