我使用以下代码更改了 Floating Action Button
backgroundTintList 颜色:
fab.setBackgroundTintList(ColorStateList.valueOf(mResources.getColor(R.color.fab_color)));
但我最终在 API 4.4.2 上得到以下信息:
在 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 code对于 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);
}
最佳答案
我结束添加:
app:borderWidth="0dp"
这不会创建 borderDrawable 并且边框不可见。
关于android - float 操作按钮边框颜色没有改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33031571/