3

I have pretty simple custom view:

public class TestControl : RelativeLayout
{
    private readonly Context _context;

    private TextView _textLabel;

    public TestControl(Context context) : base(context)
    {
        _context = context;

        LayoutParameters = new LayoutParams(300, 200);
        SetBackgroundColor(Color.Green);

        _textLabel = new TextView(_context);
        _textLabel.SetText("Test test test test test test", TextView.BufferType.Normal);
        _textLabel.SetTextColor(Android.Graphics.Color.Black);
        _textLabel.LayoutParameters = new ViewGroup.LayoutParams(200, 50);
        _textLabel.SetBackgroundColor(Color.Red);

        AddView(_textLabel);
    }

    protected override void OnLayout(bool changed, int l, int t, int r, int b)
    {

    }
}

When I try to add it as a view into my main activity layout:

var myControl = new TestControl(this);
myMainLayout.AddView(myControl);

I see only green recangle(300 x 200) but no TextView.

What am I doing wrong? Any help on how to show at least TextView inside some colored layout much appreciated, as I need actualy much more complicated custom view.

Thanks in advance.

Oleks
  • 31,955
  • 11
  • 77
  • 132

1 Answers1

0

You'll probably need to do something similar to what's explained here. SetWillNotDraw can play some tricks on you in such circumstances.

Community
  • 1
  • 1
Narcís Calvet
  • 7,304
  • 5
  • 27
  • 47