android – 浮动操作按钮边框颜色不会改变

前端之家收集整理的这篇文章主要介绍了android – 浮动操作按钮边框颜色不会改变前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用以下代码更改了浮动操作按钮backgroundTintList颜色:
  1. fab.setBackgroundTintList(ColorStateList.valueOf(mResources.getColor(R.color.fab_color)));

但我最终在API 4.4.2上得到以下内容

在API 21上的一切看起来都很好< =但是在API 21之下的任何东西,我对FAB都有这个问题. 我是以编程方式创建FAB,如下所示:

  1. FloatingActionButton fab = new FloatingActionButton(this);
  2. CoordinatorLayout.LayoutParams layoutParams = new CoordinatorLayout.LayoutParams(
  3. ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
  4. fab.setLayoutParams(layoutParams);
  5. layoutParams.rightMargin = mResources.getDimensionPixelSize(R.dimen.activity_horizontal_margin);
  6. ((CoordinatorLayout) findViewById(R.id.coordinatorLayout)).addView(fab);
  7.  
  8. CoordinatorLayout.LayoutParams p = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
  9. p.setAnchorId(R.id.appBarLayout);
  10. p.anchorGravity = Gravity.BOTTOM | Gravity.END;
  11. fab.setLayoutParams(p);
  12. fab.setVisibility(View.VISIBLE);
  13. fab.setBackgroundTintList(ColorStateList.valueOf(mResources.getColor(R.color.fab_color)));
  14. fab.setImageDrawable(getResources().getDrawable(R.drawable.ic_button));

我也碰巧在FloatingActionButton的官方source code上运行,我看到他们在这里实例化borderDrawable:

  1. @Override
  2. void setBackgroundDrawable(Drawable originalBackground,ColorStateList backgroundTint,PorterDuff.Mode backgroundTintMode,int rippleColor,int borderWidth) {
  3. // Now we need to tint the original background with the tint
  4. mShapeDrawable = DrawableCompat.wrap(originalBackground.mutate());
  5. DrawableCompat.setTintList(mShapeDrawable,backgroundTint);
  6. if (backgroundTintMode != null) {
  7. DrawableCompat.setTintMode(mShapeDrawable,backgroundTintMode);
  8. }
  9.  
  10. final Drawable rippleContent;
  11. if (borderWidth > 0) { // BORDER DRAWABLE RIGHT HERE!!
  12. mBorderDrawable = createBorderDrawable(borderWidth,backgroundTint);
  13. rippleContent = new LayerDrawable(new Drawable[]{mBorderDrawable,mShapeDrawable});
  14. } else {
  15. mBorderDrawable = null;
  16. rippleContent = mShapeDrawable;
  17. }
  18.  
  19. mRippleDrawable = new RippleDrawable(ColorStateList.valueOf(rippleColor),rippleContent,null);
  20.  
  21. mShadowViewDelegate.setBackgroundDrawable(mRippleDrawable);
  22. mShadowViewDelegate.setShadowPadding(0,0);
  23. }

解决方法

只需更改样式文件中的coloraccent
  1. <item name="colorAccent">@color/colorAccent</item>

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

编辑:okk ..这里是你可以做的另一种选择..在你的xml中定义这个FAB

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

并且它将进行更改,然后您不需要以编程方式执行.

猜你在找的Android相关文章