android - float 操作按钮边框颜色没有改变

标签 android floating-action-button androiddesignsupport

我使用以下代码更改了 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 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/

相关文章:

android - 如何在不使用 nestedScrollView 的情况下在 webview 滚动时隐藏和显示 FAB

android - float 操作按钮不适用于 ListView

java - 获取错误膨胀类 android.support.design.widget.NavigationView

android - 如何在特定 fragment 上隐藏 FAB

android - BottomSheetBehavior启用拖动到顶部

android-recyclerview - 如何防止 AppBarLayout 展开?

android - 如何在 MVVM 架构中使用来自 onActivityResult 的数据?

android - 运行Gradle Flutter Android Studio时出错

Android - 一个对话框中的一个日期和两个时间选择器

android - 导入openCV总是显示错误