Xamarin突然挤压FAB图标

时间:2018-11-22 03:34:42

标签: c# xamarin.forms floating-action-button custom-renderer

我已经从Xamarin Forms 2.5升级到3.4,我开始仅在Android上出现FAB按钮问题。该按钮首次显示为正确,但是当我按下并弹出几次后(在包含该按钮的页面顶部),该按钮突然缩小了约40%

这是我的渲染器代码:

using Xamarin.Forms;

namespace MyApp.SLayout
{
    [Preserve(AllMembers = true)]
    public class SFloatingActionButton : Button
    {
        public static BindableProperty ButtonColorProperty = BindableProperty.Create(nameof(ButtonColor), typeof(Color),
            typeof(SFloatingActionButton), Color.Accent);

        public Color ButtonColor
        {
            get => (Color) GetValue(ButtonColorProperty);
            set => SetValue(ButtonColorProperty, value);
        }
    }
}

using System;
using Android.Content;
using Android.Content.Res;
using Android.Graphics;
using Android.Runtime;
using Android.Support.V4.View;
using FFImageLoading;
using MyApp.Droid.SLayout;
using MyApp.SLayout;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using FAB = Android.Support.Design.Widget.FloatingActionButton;

[assembly: ExportRenderer(typeof(SFloatingActionButton), typeof(FloatingActionButtonRenderer))]
namespace MyApp.Droid.SLayout
{
    [Preserve(AllMembers = true)]
    public class FloatingActionButtonRenderer : Xamarin.Forms.Platform.Android.AppCompat.ViewRenderer<SFloatingActionButton, FAB>
    {
        private readonly Context _context;
        private FAB _fab;
        private Bitmap _img;

        public FloatingActionButtonRenderer(Context context) : base(context)
        {
            _context = context;
        }

        protected override void OnElementChanged(ElementChangedEventArgs<SFloatingActionButton> e)
        {
            base.OnElementChanged(e);
            if (Control == null)
            {
                // Instantiate the native control
                _fab = new FAB(_context);
                ViewCompat.SetBackgroundTintList(_fab, ColorStateList.ValueOf(Element.ButtonColor.ToAndroid()));
                _fab.UseCompatPadding = true;
                string imageFile = Element.Image.File;
                string drawableNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(imageFile)?.ToLower();
                int imageResourceName = _context.Resources.GetIdentifier(drawableNameWithoutExtension, "drawable", _context.PackageName);
                _img = BitmapFactory.DecodeResource(_context.Resources, imageResourceName);
                _fab.SetImageBitmap(_img);
                SetNativeControl(_fab);
            }

            if (e.OldElement != null)
            {
                _img?.TryDispose();
                _fab.Click -= Fab_Click;
            }

            if (e.NewElement != null)
            {
                _fab.Click += Fab_Click;
            }
        }


        protected override void OnLayout(bool changed, int l, int t, int r, int b)
        {
            base.OnLayout(changed, l, t, r, b);
            Control.BringToFront();
        }

        private void Fab_Click(object sender, EventArgs e)
        {
            // proxy the click to the element
            ((IButtonController) Element).SendClicked();
        }
    }
}

0 个答案:

没有答案