2016-10-02 24 views
1

對於我的項目,我需要一個更大的浮動操作按鈕(FAB)(我知道它違背了Google Material Design的原則,對此做了很多)。我知道FAB對他們有默認和迷你尺寸,但我需要更大。爲了獲得更大的尺寸,我做了繼承FAB類自定義按鈕:對於Xamarin.Android和AndroidSDK API 19和更小版本的放大浮動操作按鈕的影子填充錯誤

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是FOL?低點:

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的Visual Studio版本(4.2.0.695)和最新Xamarin.Android(7.0.12)。 謝謝!編號: 這是MVCE - https://ufile.io/b691

+0

你能上傳一個MVCE(http://stackoverflow.com/help/mcve)在項目中演示這個問題嗎?您可以在帖子中添加指向託管服務提供商的鏈接。 –

+0

@JonDouglas Adeed MVCE的問題。你也可以在這裏下載它:https://ufile.io/b691 – Necroqubus

回答

1

爲什麼發生這種情況的原因是,SetMeasuredDimension()增加了對奇巧陰影填充及以下: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. 
    */ 

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

因此這可以很容易地通過去除任何將通過去除陰影的高程來修復屬性:

this.CompatElevation = 0f;

現在你大FAB應該< API 19看起來棒極了!

+0

謝謝你,它的工作原理,但當浮動操作按鈕的大小放大時還有其他問題 - 它們不再是對稱的圓形,而是橢圓形,寬度爲no等於身高。並通過SetMeasuredDimension設置寬度等於高度不起作用。任何想法,爲什麼?這個問題可以在同一個項目中複製,並且也可以在我提供的截圖中看到(如果你仔細看它的高度高於它的寬度)。 – Necroqubus

+0

打開一個新問題。原來的問題應該被認爲是解決的。 –

+0

那麼,原來的問題是爲什麼放大的浮動按鈕看起來很奇怪。其中一部分原因是由於陰影故障,你謝謝幫我解決,但一部分是由於圈不是正確的大小。我擔心如果我再提出這樣的問題,它會被認爲是重複的。 – Necroqubus