我有以下课程
TextLayout类,我在其中创建了一个textview并将其添加到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)文本在视图中呈现。
设置类的布局参数的正确方法是什么?
有没有办法解决这个问题?
答案 0 :(得分:1)
设置类的布局参数的正确方法是什么?
onMeasure旨在确定视图的测量宽度和测量高度。它将在初始化或视图大小发生变化时多次调用。因此,在onMeasure
中放置一些初始化代码并不是一个好地方。
在您的代码中,this.AddView(textLayout);
中的IntermediateLayout
会在多次调用onMeasure
时导致异常。
因此,放置视图初始化代码的好地方是在类的构造函数中:
<强> 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
}