C# 在Xamarin.Android中的OnMeasure override方法的单独布局中添加TextView时,不会呈现TextView

C# 在Xamarin.Android中的OnMeasure override方法的单独布局中添加TextView时,不会呈现TextView,c#,android,xamarin.android,onmeasure,C#,Android,Xamarin.android,Onmeasure,我有以下课程 TextLayout类,我在其中创建了一个textview,并将其添加到onMeasure override方法中 public class TextLayout : FrameLayout { private TextView headerLabel; public TextLayout(Context context) : base(context) { } private void UpdateText() {

我有以下课程

TextLayout类,我在其中创建了一个textview,并将其添加到onMeasure override方法中

public class TextLayout : FrameLayout
{
    private TextView headerLabel;
    public TextLayout(Context context) : base(context)
    {

    }
    private void UpdateText()
    {
        if(headerLabel == null)
        this.headerLabel = new TextView(this.Context);
        headerLabel.LayoutParameters = (new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent));
        this.headerLabel.Text = "General Meeting";
        headerLabel.SetTextColor(Color.Green);
        headerLabel.Typeface = Typeface.DefaultBold;
        headerLabel.TextSize = 25;
    }

    protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        base.OnMeasure(widthMeasureSpec, heightMeasureSpec);
        UpdateText();
        int minimumWidth = this.PaddingLeft + PaddingRight + SuggestedMinimumWidth;
        int widthOfLayout = ResolveSizeAndState(minimumWidth, widthMeasureSpec, 1);
        SetMeasuredDimension(widthOfLayout, 75);
        if (this.headerLabel.Parent == null)
        {
            this.AddView(this.headerLabel);
        }
    }
}
我有另一个类作为中间类,我在这个中间类的OnMeasure方法中添加了TextLayout类,如下所示

public class IntermediateLayout : FrameLayout
{
    TextLayout textLayout;
    public IntermediateLayout(Context context) : base(context)
    {

    }

    protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        base.OnMeasure(widthMeasureSpec, heightMeasureSpec);
        if (textLayout == null)
            textLayout = new TextLayout(this.Context);
        this.AddView(textLayout);
    }
}
最后,在main类中,我在OnCreate的main类中添加了中间类

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        IntermediateLayout mainLayout = new IntermediateLayout(this);

        // Set our view from the "main" layout resource
        SetContentView(mainLayout);
    }
在此场景中,textview不会呈现在Xamarin.Android平台中的视图中

同样在另一个场景中,当在中间类(定义了TextClass)的OnMeasure方法中设置TextLayout类的布局参数时,文本不会呈现在视图中。但当在同一类(TextLayout)的OnMeasure方法中设置布局参数时,文本会呈现在视图中

设置类的布局参数的正确方法是什么

有没有办法解决这个问题

设置类的布局参数的正确方法是什么

旨在确定视图的测量宽度和测量高度。初始化时或视图大小更改时,将多次调用它。因此,将一些初始化代码放在测量中不是一个好地方

在代码中,
this.AddView(textLayout)onMeasure
时,
IntermediateLayout
中的code>将导致异常

因此,放置视图初始化代码的好地方是类的构造函数:

TextLayout.cs:

public class TextLayout:FrameLayout
{
    private TextView headerLabel;
    public TextLayout(Context context) : base(context)
    {
        //put the initialization codes in contructor
            UpdateText();
            if (this.headerLabel.Parent == null)
            {
                this.AddView(this.headerLabel);
            }
    }


    private void UpdateText()
    {
        if (headerLabel == null)
            this.headerLabel = new TextView(this.Context);
        headerLabel.LayoutParameters = (new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent));
        this.headerLabel.Text = "General Meeting";
        headerLabel.SetTextColor(Color.Green);
        headerLabel.Typeface = Typeface.DefaultBold;
        headerLabel.TextSize = 25;
    }


    protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        base.OnMeasure(widthMeasureSpec, heightMeasureSpec);
        int minimumWidth = this.PaddingLeft + PaddingRight + SuggestedMinimumWidth;
        int widthOfLayout = ResolveSizeAndState(minimumWidth, widthMeasureSpec, 1);
        SetMeasuredDimension(widthOfLayout, 75);
    }
}
IntermediateLayout.cs:

public class IntermediateLayout : FrameLayout
{
    TextLayout textLayout;
    public IntermediateLayout(Context context) : base(context)
    {
        if (textLayout == null)
            textLayout = new TextLayout(this.Context);
        this.AddView(textLayout);
    }
    //there is no need to override onMeasured here
}