I have following classes
TextLayout class, in which I have created a single textview and added it in onMeasure override method.
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);
}
}
}
I have another class as an intermediate class, in which I have added the TextLayout class in OnMeasure method of this intermediate class as below,
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);
}
}
Finally, in the main class I have added the intermediate class in the OnCreate of main class.
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
IntermediateLayout mainLayout = new IntermediateLayout(this);
// Set our view from the "main" layout resource
SetContentView(mainLayout);
}
In this scenario, the textview is not rendered the view in Xamarin.Android platform.
Also in another scenario,when layout parameters for TextLayout class is set in the OnMeasure method of Intermediate class(where TextClass is defined), text is not rendered in the view.But when the layout parameters is set in the OnMeasure method of same class(TextLayout) text is rendered in the view.
Which is the correct way to set layout parameters of a class?
Is there any way to fix this problem?