繁体   English   中英

带有 AndroidSDK API 19 及更低版本的 Xamarin.Android 的放大浮动操作按钮的阴影填充错误

[英]Shadow Padding bug for enlarged Floating Action Button for Xamarin.Android with AndroidSDK API 19 and less

对于我的项目,我需要一个更大的浮动操作按钮 (FAB)(我知道这违反了 Google Material Design 的原则,但对此无能为力)。 我知道 FAB 有默认尺寸和迷你尺寸,但我需要更大的尺寸。 为了获得更大的尺寸,我制作了继承 FAB 类的自定义按钮类:

using System;
using Android.Content;
using Android.Content.Res;
using Android.Graphics;
using Android.Graphics.Drawables;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Java.Lang;
using Android.Support.Design.Widget;

public class BiggerFloatingActionButton : FloatingActionButton
{
    [Register(".ctor", "(Landroid/content/Context;)V", "")]
    public BiggerFloatingActionButton(Context context) : base(context)
    {
    }
    [Register(".ctor", "(Landroid/content/Context;Landroid/util/AttributeSet;)V", "")]
    public BiggerFloatingActionButton(Context context, IAttributeSet attrs) : base(context, attrs) { }
    [Register(".ctor", "(Landroid/content/Context;Landroid/util/AttributeSet;I)V", "")]
    public BiggerFloatingActionButton(Context attrscontext, IAttributeSet attrs, int defStyleAttr) : base(attrscontext, attrs, defStyleAttr) { }
    protected BiggerFloatingActionButton(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer) { }

    protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        base.OnMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = this.MeasuredWidth;
        int height = this.MeasuredHeight;

        this.SetMeasuredDimension((int)(width * 1.75f), (int)(height * 1.75f));
        //base.OnMeasure(width, height);
    }
}

结果,它有效,但在 Android API 19 上存在阴影填充问题,并且可能更低:

在 Android API 20 上它看起来不错:”

有谁知道为什么会发生前一种效果的线索?

我的自定义 FAB 的用法如下:

var buttons = new List<BiggerFloatingActionButton>();
foreach (var item in categories)
{
    var button = new BiggerFloatingActionButton(this);
    var iconIdentifier = this.Resources.GetIdentifier(item.Icon, "drawable", this.ApplicationContext.PackageName);
    var drawable = ContextCompat.GetDrawable(this, iconIdentifier);
    button.SetImageDrawable(drawable);
    buttons.Add(button);
}

var layout = this.FindViewById<LinearLayout>(Resource.Id.linear_main);
foreach(var button in buttons)
{
    layout.AddView(button);
}

作为参考,我使用最新的 Xamarin for Visual Studio 版本 (4.2.0.695) 和最新的 Xamarin.Android (7.0.12)。 谢谢!

编辑:这是 MVCE - https://ufile.io/b691

发生这种情况的原因是SetMeasuredDimension()在 KitKat 及以下添加了阴影填充: https : SetMeasuredDimension() /FloatingActionButton.java#200

您可以看到以下评论:

 /** * Pre-Lollipop we use padding so that the shadow has enough space to be drawn. This method * offsets our layout position so that we're positioned correctly if we're on one of * our parent's edges. */

https://android.googlesource.com/platform/frameworks/support/+/master/design/src/android/support/design/widget/FloatingActionButton.java#729

因此,这可以通过移除任何高程来轻松解决,这将通过CompatElevation属性移除阴影:

this.CompatElevation = 0f;

现在您的大型 FAB 在 < API 19 上应该看起来很棒!

暂无
暂无

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

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