我有以下课程
TextLayout 类,我在其中创建了一个文本视图并将其添加到 onMeasure 覆盖方法中。
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);
}
}
最后,在主类中,我在主类的 OnCreate 中添加了中间类。
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 平台中呈现视图。
同样在另一种情况下,当TextLayout类的布局参数在Intermediate类的OnMeasure方法中设置时(其中定义了TextClass),文本不会在视图中呈现。但是当布局参数在同一类的OnMeasure方法中设置时( TextLayout) 文本在视图中呈现。
设置类的布局参数的正确方法是什么?
有没有办法解决这个问题?