【问题标题】:MonoDroid custom View doesn't show childMonoDroid 自定义视图不显示子项
【发布时间】:2012-07-04 16:02:05
【问题描述】:

我有非常简单的自定义视图:

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)
    {

    }
}

当我尝试将其作为视图添加到我的主要活动布局中时:

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

我只看到绿色矩形(300 x 200)但没有 TextView。

我做错了什么?任何关于如何在一些彩色布局中至少显示 TextView 的帮助都非常感谢,因为我实际上需要更复杂的自定义视图。

提前致谢。

【问题讨论】:

    标签: android custom-controls xamarin.android


    【解决方案1】:

    您可能需要执行类似于here 中解释的操作。 SetWillNotDraw 在这种情况下可以捉弄你。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多