繁体   English   中英

为什么自定义视图的子视图不会在棉花糖中渲染?

[英]Why won't the child views of my custom view render in Marshmallow?

我正在使用Xamarin开发自定义键盘。

我的键盘视图的视图容器本身及其子键视图都有一个重写的OnDraw()。 我还为每个视图适当地使用SetWillNotDraw(false)。 目前,在我的Nexus 10平板电脑上的5.0.1版本中,它运行良好。

在Android 6.0.1中,在Nexus 6和Nexus 6P上,键盘视图会正确绘制自身(只是背景颜色)。 但是,即使我遍历视图层次结构并对每个视图强制无效,也不会绘制子键视图。 这似乎是棉花糖特有的。

我不知道在此版本的Android中是否需要解决一些新问题,或者是否遇到错误。

欢迎任何帮助或建议。

码:

KeyboardView

的KeyView

一些额外的细节可以阐明原始帖子:

我们用于键盘渲染的三个主要文件是KeyboardView.csKeyboardRowView.csKeyView.cs

KeyboardView (整个键盘的容器)

渲染没有问题。 KeyboardView扩展了LinearLayout并运行其OnDraw方法,并调用Build()函数来创建所需的内容(只是一个基本背景,它将“保留”各个键):

protected override void OnDraw(Canvas canvas)
    {
        Build(); 

        base.OnDraw(canvas);

        // background
        Paint bg = new Paint(PaintFlags.AntiAlias);
        bg.Color = BG; // light blue
        canvas.DrawRect(0, 0, MeasuredWidth, Height, bg);

        InvalidateKeys();
    }

(...以及下面的Build() ...)

public void Build()
    {
        // only build once
        if (keyLayout != null)
            return;

        // clear out children
        RemoveAllViews();

        // define sizes of stuff
        if (isPortrait)
        {
            keyMargin = (int)(MeasuredWidth * .01f);
        }
        else
        {
            keyMargin = (int)(MeasuredHeight * .01f);
        }

        keyWidth = (MeasuredWidth - (keyMargin * 2)) / keyboard.MaxCols;
        keyHeight = (MeasuredHeight - (keyMargin * 2)) / keyboard.Rows.Count;

        // set general padding around keyboardview
        SetPadding(keyMargin, keyMargin, keyMargin, keyMargin);

        // build KeyLayout from the keyboard object
        keyLayout = new List<List<KeyView>>();
        int idx = 0;
        foreach (List<Key> row in keyboard.Rows)
        {
            keyLayout.Add(new List<KeyView>());

            // create and add new KeyboardRowView
            KeyboardRowView krv = new KeyboardRowView(Context, this, idx);
            AddView(krv);

            // figure out if we need a margin offset for this row
            int extraMargin = 0;
            int numCols = CountRowCols(row);
            if (numCols < keyboard.MaxCols)
            {
                // measure full width of the button container and the total row margin
                int rowWidth = (int)(numCols * keyWidth);
                int rowMargin = MeasuredWidth - (keyMargin * 2) - rowWidth;

                // add the offset
                extraMargin = rowMargin / 2;
            }

            // build keys and add them to keyLayout and KeyboardRowView
            int idx2 = 0;
            foreach (Key key in row)
            {
                int leftMargin = idx2 == 0 ? extraMargin : 0;
                KeyView kv = new KeyView(Context, this, key, leftMargin);
                keyLayout[idx].Add(kv);
                krv.AddView(kv);

                idx2++;
            }

            idx++;
        }
    }

(提醒您,我们这样做是因为我们需要一个自定义键盘,该键盘只能向用户显示某些键/命令。)

KeyboardRowView (每行按键的容器)

这也扩展了LinearLayout ,还具有其OnDraw方法,称为:

protected override void OnDraw(Canvas canvas)
    {
        base.OnDraw(canvas);
        Paint paint = new Paint();
        paint.SetARGB(255, 0, 0, 0);
        paint.SetStyle(Paint.Style.Stroke);
        paint.StrokeWidth = 3;
        canvas.DrawRGB(255, 255, 255);
        canvas.DrawRect(0, 0, 100, 100, paint);
    }

KeyView (加载和呈现每个单独键的类)

KeyView扩展了ViewView.IOnTouchListener 调用了KeyView的构造函数,但从未调用/执行过其OnDraw方法:

// key views are always dynamically created
    public KeyView(Context ctx, KeyboardView parent, Key k, int leftMargin)
        : base(ctx)
    {
        // make sure the key will draw
        SetWillNotDraw(false);

        keyboard = parent;
        key = k;
        isDown = false;

        // check for an overridden span to adjust width, if needed
        int span = string.IsNullOrEmpty(key.Span) ? 1 : Convert.ToInt32(key.Span);
        int keyWidth = keyboard.keyWidth + ((span - 1) * keyboard.keyWidth);

        width = keyWidth;
        height = keyboard.keyHeight;

        // set margin
        var parameters = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WrapContent,
            LinearLayout.LayoutParams.MatchParent
        );
        parameters.LeftMargin = leftMargin;
        LayoutParameters = parameters;

        // set touch listener
        SetOnTouchListener(this);

        // enable haptic feedback for button presses
        HapticFeedbackEnabled = true;
    }

(...和OnDraw)

protected override void OnDraw(Canvas canvas)
    {
        base.OnDraw(canvas);

        KeyState primary = key.Primary;
        KeyState secondary = key.Secondary;

        if (keyboard.swapped)
        {
            primary = key.Secondary != null ? key.Secondary : key.Primary;
            secondary = key.Secondary != null ? key.Primary : null;
        }

        if (keyboard.shifted)
        {
            if (primary.Shift != null)
                primary = primary.Shift;

            if (secondary != null && secondary.Shift != null)
                secondary = secondary.Shift;
        }

        // figure out what color the key is supposed to be
        Paint bg = new Paint(PaintFlags.AntiAlias);
        bg.Color = GetKeyBgColor(key.Style);
        if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Lollipop)
            canvas.DrawRoundRect(keyboard.keyMargin, keyboard.keyMargin, width - keyboard.keyMargin, height - keyboard.keyMargin, keyboard.keyMargin, keyboard.keyMargin, bg);
        else
            canvas.DrawRoundRect(new RectF(keyboard.keyMargin, keyboard.keyMargin, width - keyboard.keyMargin, height - keyboard.keyMargin), keyboard.keyMargin, keyboard.keyMargin, bg);

        // draw primary key state
        Paint fg = new Paint(PaintFlags.AntiAlias);
        fg.TextSize = height * .5f;
        fg.Color = GetKeyFgColor(key.Style);
        string character = string.IsNullOrEmpty(primary.Character) ? "#" : primary.Character;
        int charWidth = Convert.ToInt32(fg.MeasureText(character));
        int charX = (width - charWidth) / 2;
        canvas.DrawText(character, charX, (height * .7f), fg);

        // draw secondary key state
        if (secondary != null)
        {
            fg.TextSize = height * .25f;
            fg.Color = GetKeyFgColor(key.Style, true);
            character = string.IsNullOrEmpty(secondary.Character) ? "#" : secondary.Character;
            charWidth = Convert.ToInt32(fg.MeasureText(character));
            charX = width - charWidth - (keyboard.keyMargin * 2);
            canvas.DrawText(character, charX, (height * .35f), fg);
        }
    }

我很困惑。 KeyboardViewKeyboardRowView都具有SetWillNotDraw(false); 在其构造函数/初始化方法中调用函数。 KeyView也具有相同的函数调用,并成功接收需要呈现的每个键值。 我不明白的是为什么它只是...不会...绘制...键盘。 (Argh。)当我与原始海报商谈此事时,他告诉我所有条件都已得到满足,以便呈现键盘键。 我尝试附加断点以查看阻止调用KeyView的OnDraw的原因,但是陷入了重复的OnMeasure函数调用中(并且渲染了很多键,因此很快就变老了)。

值得一提的是,我们已经在最新的Nexus 6P智能手机(运行时的Android 6.0棉花糖) 旧版的Motorola Droid 4(通过CyanogenMod 13安装了棉花糖)上进行了测试。 当我们使用Xamarin Android Player模拟器(运行棉花糖)进行尝试时,它实际上可以工作 ……我的猜测是,模拟器可能毫无问题地渲染了键盘,因为实际的手机本身就是

(a)以某种方式限制访问

(b)可能会保留旧代码,而我们还没有完全删除它们的旧.apks

(c)我没有想到的其他问题

感谢您的时间。 如果有人能想到一个可行的方向,将不胜感激!

通过使用Android文档建议的自上而下方法为每个自定义视图正确实现OnMeasure和OnLayout进行了修复。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM