【问题标题】:Floating Action Button Border Color is not changing浮动操作按钮边框颜色不变
【发布时间】:2016-01-07 00:10:20
【问题描述】:

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

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

但我最终在 API 4.4.2 上得到以下信息:

在 API 21 上一切正常

我正在以编程方式创建 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


    【解决方案1】:

    我认为有些人仍然会遇到这种情况,所以它可能对某人有所帮助

    在属性部分有两个 backgroundTint 属性,第二个是用于

    更改边框颜色希望对某人有所帮助

    【讨论】:

      【解决方案2】:

      我结束添加:

      app:borderWidth="0dp"
      

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

      【讨论】:

        【解决方案3】:

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

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

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

        【讨论】:

          【解决方案4】:

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

          这些属性用于在单击和提升按钮时赋予视图效果。

          【讨论】:

          • 这适用于边框!命名空间中“android”和“app”的行为是不同的。将其设置为 app:backgroundTint 会更改边框颜色。谢谢。
          【解决方案5】:

          只需更改样式文件中的颜色强调

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

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

          编辑:好的..这是你可以做的另一种选择..在你的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" />
          

          它会进行更改,然后您就不需要以编程方式进行操作了。

          【讨论】:

          • 我在应用程序中有其他视图必须使用特定的颜色强调,所以这个选项是不可行的,并且不是真正可扩展的。这更像是一个“核”选项,我宁愿不这样做。
          • 这不是建设性的。
          • @AndyRoid 检查一旦我在回答中添加了我的另一个建议
          • 这对问题没有用。
          猜你喜欢
          • 2019-01-16
          • 2015-09-07
          • 2018-12-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-12-11
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多