浮动动作button边框颜色不变

我通过使用以下代码更改了Floating Action Button backgroundTintList颜色:

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

但是我最终得到了API 4.4.2的以下内容:

在这里输入图像说明

一切看起来很好API 21 <=但API 21以下,我有这个问题的FAB。

我正在编程创buildFAB,如下所示:

  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)); 

我也碰巧由FloatingActionButton的官方源代码运行,我看到他们在这里实例化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); } 

Solutions Collecting From Web of "浮动动作button边框颜色不变"

只是改变样式文件中的颜色

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

添加你想要的颜色作为FAB的背景颜色

编辑:okk ..以及这里是一个替代你可以做..在你的xml中定义这个FAB

  <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" /> 

它会做出改变,然后你不需要按照程序来做。

要更改背景颜色,请使用: 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" /> 

这些属性是用来给点击和抬高button的查看效果。

您可能需要以向后兼容的方式以编程方式更改颜色:

DrawableCompat.setTintList(DrawableCompat.wrap(fab.getDrawable()), tintColor); < – 图标

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