gpt4 book ai didi

c# - 在 Xamarin.Android 中的 OnMeasure 覆盖方法中添加到单独的布局中时,不会呈现 TextView

转载 作者:行者123 更新时间:2023-11-30 00:29:41 25 4
gpt4 key购买 nike

我有以下类(class)

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 平台中的 View 。

同样在另一种情况下,当在Intermediate类(其中定义了TextClass)的OnMeasure方法中设置TextLayout类的布局参数时,文本不会在 View 中呈现。但是当在 OnMeasure 方法中设置布局参数时在 View 中呈现相同类 (TextLayout) 的文本。

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

有什么办法可以解决这个问题吗?

最佳答案

Which is the correct way to set layout parameters of a class?

onMeasure旨在确定 View 的测量宽度和测量高度。它会在初始化时或 View 大小发生变化时被多次调用。所以在onMeasure中放置一些初始化代码不是一个好地方。

在你的代码中, IntermediateLayout 中的 this.AddView(textLayout); 会在多次调用 onMeasure 时导致异常.

所以放置 View 初始化代码的好地方是在类的构造函数中:

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
}

关于c# - 在 Xamarin.Android 中的 OnMeasure 覆盖方法中添加到单独的布局中时,不会呈现 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44668693/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com