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

标签 c# android xamarin.android onmeasure

我有以下类(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/

相关文章:

c# - FakeItEasy Proxy 方法调用真实实现

java - Android ksoap 中的安全 header

android - 大多数平台版本不可知的方式来获取特定的文件路径?

java - Java.Net.Uri 和 Android.Net.Uri 有什么区别

c# - 如何从聚焦的 Internet Explorer 浏览器窗口获取 URL

c# - 找不到 StructureMap 可插入类型或命名空间

c# - Unity(Android Studio)重复录入错误

c# - 自定义 WebView 在模拟器中工作但不在物理设备中工作。赛马林

c# - 使用 asp.net mvc 4 + Entity Framework 将图像保存到数据库

Android Studio 1.3 RC3。 Google Play 服务已过时。需要 7571000 但找到 6774470