简体   繁体   中英

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

I'm developing a custom keyboard using Xamarin.

My keyboard view has an overridden OnDraw() for both the view container itself and it's child key views. I'm also using SetWillNotDraw(false) appropriately for each view. It currently works beautifully in 5.0.1 on my Nexus 10 tablet.

In Android 6.0.1, on a Nexus 6 and a Nexus 6P, the keyboard view correctly draws itself (just a background color). The child key views however are never drawn, even if I iterate through the view hierarchy and force an invalidate on each one. This seems to be specific to Marshmallow.

I don't know if there's something new I need to account for in this version of Android or if I'm encountering a bug.

Any help or suggestions are welcome.

Code:

KeyboardView

KeyView

Some extra details to shed light on the original post:

The three major files we use for keyboard rendering are KeyboardView.cs , KeyboardRowView.cs , and KeyView.cs .

KeyboardView (the container for the whole keyboard)

This has no trouble rendering. KeyboardView extends a LinearLayout and its OnDraw method runs, calling a Build() function to create what it needs (just a basic background which will "hold" the individual keys):

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();
    }

(...and Build() below...)

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++;
        }
    }

(As a friendly reminder, we're doing this because we need a custom keyboard which can only display certain keys/commands to our users.)

KeyboardRowView (the container for each row of keys)

This also extends a LinearLayout , and also has its OnDraw method called:

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 (the class which loads and renders each individual key)

KeyView extends View and View.IOnTouchListener . KeyView's constructor is called, but its OnDraw method is never called/executed:

// 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;
    }

(...and 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);
        }
    }

I am confused. Both KeyboardView and KeyboardRowView have a SetWillNotDraw(false); function call in their constructor/initialization methods. KeyView also has the same function call, and successfully receives each key value that needs to be rendered. What I don't get is why it just...won't...draw...the...keyboard. (Argh.) When I spoke with the original poster about this, he told me that all the conditions have been met in order for the keyboard keys to be rendered. I tried attaching breakpoints to see what was preventing KeyView's OnDraw from being called, but got caught up in repeated OnMeasure function calls (and there are a lot of keys that get rendered, so that got old quick ).

It's worth mentioning that we've tested it on the latest Nexus 6P smartphone (running stock Android 6.0 Marshmallow) and an old Motorola Droid 4 (with Marshmallow installed via CyanogenMod 13). When we tried it using a Xamarin Android Player emulator (running Marshmallow), it actually worked ... my guess is that the emulator might be rendering the keyboard with no problem because the actual phones themselves are either

(a) restricting access somehow

(b) potentially holding on to old code and we just haven't completely removed their old .apks

(c) some other issue I haven't thought of

Thank you for your time. If anyone can think of a possible direction to go in, it would be appreciated!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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