浮动操作按钮边框颜色不会更改

问题描述 投票:7回答:4

我使用以下代码更改了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);
}
android floating-action-button androiddesignsupport
4个回答
12
投票

我结束了添加:

app:borderWidth="0dp"

这不会创建borderDrawable并且边框不可见。


7
投票

只需更改样式文件中的coloraccent

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

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


2
投票

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

DrawableCompat.setTintList(DrawableCompat.wrap(fab.getDrawable()), tintColor);

DrawableCompat.setTintList(DrawableCompat.wrap(fab.getBackground()), backgroundTintColor);


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

这些属性用于为按钮提供单击和提升的视图效果。

© www.soinside.com 2019 - 2024. All rights reserved.