2015-10-09 190 views
7

我用下面的代碼改變了Floating Action Button backgroundTintList顏色:浮動操作按鈕邊框顏色是不會改變

fab.setBackgroundTintList(ColorStateList.valueOf(mResources.getColor(R.color.fab_color))); 

但我最終得到的API 4.4.2如下:

enter image description here

API 21 < =一切看起來都不錯,但是API 21以下的任何東西,我對FAB都有這個問題。

我編程方式創建的FAB像這樣:

FloatingActionButton fab = new FloatingActionButton(this); 
    CoordinatorLayout.LayoutParams layoutParams = new CoordinatorLayout.LayoutParams(
      ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
    fab.setLayoutParams(layoutParams); 
    layoutParams.rightMargin = mResources.getDimensionPixelSize(R.dimen.activity_horizontal_margin); 
    ((CoordinatorLayout) findViewById(R.id.coordinatorLayout)).addView(fab); 

    CoordinatorLayout.LayoutParams p = (CoordinatorLayout.LayoutParams) fab.getLayoutParams(); 
    p.setAnchorId(R.id.appBarLayout); 
    p.anchorGravity = Gravity.BOTTOM | Gravity.END; 
    fab.setLayoutParams(p); 
    fab.setVisibility(View.VISIBLE); 
    fab.setBackgroundTintList(ColorStateList.valueOf(mResources.getColor(R.color.fab_color))); 
    fab.setImageDrawable(getResources().getDrawable(R.drawable.ic_button)); 

我也正好由官方source codeFloatingActionButton運行,我看到他們在這裏實例化一個borderDrawable:

@Override 
void setBackgroundDrawable(Drawable originalBackground, ColorStateList backgroundTint, 
     PorterDuff.Mode backgroundTintMode, int rippleColor, int borderWidth) { 
    // Now we need to tint the original background with the tint 
    mShapeDrawable = DrawableCompat.wrap(originalBackground.mutate()); 
    DrawableCompat.setTintList(mShapeDrawable, backgroundTint); 
    if (backgroundTintMode != null) { 
     DrawableCompat.setTintMode(mShapeDrawable, backgroundTintMode); 
    } 

    final Drawable rippleContent; 
    if (borderWidth > 0) { // BORDER DRAWABLE RIGHT HERE!! 
     mBorderDrawable = createBorderDrawable(borderWidth, backgroundTint); 
     rippleContent = new LayerDrawable(new Drawable[]{mBorderDrawable, mShapeDrawable}); 
    } else { 
     mBorderDrawable = null; 
     rippleContent = mShapeDrawable; 
    } 

    mRippleDrawable = new RippleDrawable(ColorStateList.valueOf(rippleColor), 
      rippleContent, null); 

    mShadowViewDelegate.setBackgroundDrawable(mRippleDrawable); 
    mShadowViewDelegate.setShadowPadding(0, 0, 0, 0); 
} 

回答

7

只是改變風格文件

<item name="colorAccent">@color/colorAccent</item> 

加入其中U要作爲背景色FAB

顏色

編輯:OKK ..好這裏是一種替代你可以做什麼..在你的XML

<android.support.design.widget.FloatingActionButton 
    android:id="@+id/fab" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom|end" 
    app:backgroundTint="@color/fab_color" 
    android:layout_margin="@dimen/fab_margin" 
    android:src="@android:drawable/ic_dialog_email" /> 

定義這個FAB,它會做出改變然後你不需要按照程序來做。

+2

我在應用程序中有其他意見,必須使用特定的顏色重音,所以此選項是一個不行,並不是真正的可擴展性。這更像是一個'核'選項,我寧願不這樣做。 – AndyRoid

+1

這不是建設性的。 – AndyRoid

+0

@AndyRoid檢查一次,我在回答中添加了我的另一個建議 – curiousMind

1

只需更改背景顏色的使用:app:backgroundTint="#4000FF00"

如:

<android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="54dp" 
     android:layout_marginRight="16dp" 
     android:clickable="true" 
     android:src="@drawable/ic_edit" 
     app:layout_anchor="@id/xxxx" 
     app:rippleColor="@android:color/white" 
     app:backgroundTint="#00FF00" 
     app:layout_anchorGravity="bottom|end|right" 
     /> 

但是,如果你想讓它透明,使用app:elevationapp:pressedTranslationZ財產。

如:

<android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="54dp" 
     android:layout_marginRight="16dp" 
     android:clickable="true" 
     android:src="@drawable/ic_edit" 
     app:layout_anchor="@id/xxx" 
     app:borderWidth="0dp" 
     app:rippleColor="@android:color/white" 
     app:backgroundTint="#4000FF00" 
     app:elevation="0dp" 
     app:pressedTranslationZ="0dp" 
     app:layout_anchorGravity="bottom|end|right" 
     /> 

這些屬性是用來給上點擊海拔按鈕查看效果。

+2

只適用於API 21而不是有效的解決方案 – AndyRoid

1

你可能需要以編程方式更改顏色以向後兼容的方式:

DrawableCompat.setTintList(DrawableCompat.wrap(fab.getDrawable()), tintColor); < - 圖標

DrawableCompat.setTintList(DrawableCompat.wrap(fab.getBackground()), backgroundTintColor); < - 背景

+0

非常有用!謝謝 –

1

我最後補充道:

app:borderWidth="0dp" 

這不會創建borderDrawable,邊框不可見。