对于我的项目,我需要一个更大的浮动操作按钮(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上存在阴影填充问题,可能更低: http://image.prntscr.com/image/40f115e8e38c466196887f475ebd7726.png
在Android API 20上,它看起来还不错:" http://image.prntscr.com/image/d80110bc80d244b9be71fe30b1534188.png
有谁知道前一种效应发生的线索?
我的自定义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。
答案 0 :(得分:1)
发生这种情况的原因是SetMeasuredDimension()
在KitKat及其下方添加了阴影填充:https://android.googlesource.com/platform/frameworks/support/+/master/design/src/android/support/design/widget/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. */
因此,可以通过删除任何将通过CompatElevation
属性删除阴影的高程来轻松修复此问题:
this.CompatElevation = 0f;
现在你的大型FAB在&lt; API 19!