Android 更改浮动操作按钮颜色 [英] Android changing Floating Action Button color

查看:35
本文介绍了Android 更改浮动操作按钮颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试更改 Material 的浮动操作按钮颜色,但没有成功.

I have been trying to change Material's Floating Action Button color, but without success.

<android.support.design.widget.FloatingActionButton
    android:id="@+id/profile_edit_fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end|bottom"
    android:layout_margin="16dp"
    android:clickable="true"
    android:src="@drawable/ic_mode_edit_white_24dp" />

我尝试添加:

android:background="@color/mycolor"

或通过代码:

FloatingActionButton fab = (FloatingActionButton) rootView.findViewById(R.id.profile_edit_fab);
fab.setBackgroundColor(Color.parseColor("#mycolor"));

fab.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#mycolor")));

但以上都没有奏效.我也尝试过提出的重复问题中的解决方案,但没有一个有效;按钮保持绿色,也变成了正方形.

But none of the above worked. I have also tried the solutions in the proposed duplicate question, but none of them works; the button remained green and also became a square.

附言知道怎么加涟漪效果也不错,也看不懂.

P.S. It would be also nice to know how to add ripple effect, couldn't understand that either.

推荐答案

文档,默认情况下它采用 styles.xml 属性 colorAccent 中设置的颜色>.

As described in the documentation, by default it takes the color set in styles.xml attribute colorAccent.

此视图的背景颜色默认为您主题的 colorAccent.如果您希望在运行时更改此设置,则可以通过 setBackgroundTintList(ColorStateList) 进行.

The background color of this view defaults to the your theme's colorAccent. If you wish to change this at runtime then you can do so via setBackgroundTintList(ColorStateList).

如果你想改变颜色

  • 在带有属性 app:backgroundTint
  • 的 XML 中
  • in XML with attribute app:backgroundTint
<android.support.design.widget.FloatingActionButton
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_add"
    app:backgroundTint="@color/orange"
    app:borderWidth="0dp"
    app:elevation="6dp"
    app:fabSize="normal" >

  • 在带有 .setBackgroundTintList 的代码中(下面由 ywynm)
    • in code with .setBackgroundTintList (answer below by ywwynm)
    • 正如@Dantalian 在评论中提到的,如果您希望更改设计支持库的图标颜色最高 v22(含),您可以使用

      As @Dantalian mentioned in the comments, if you wish to change the icon color for Design Support Library up to v22 (inclusive), you can use

      android:tint="@color/white"     
      

      对于设计支持库自 v23,您可以使用:

      For Design Support Library since v23 for you can use:

      app:tint="@color/white"   
      

      还有 androidX 库,您需要在 xml 布局中设置 0dp 边框:

      Also with androidX libraries you need to set a 0dp border in your xml layout:

      <com.google.android.material.floatingactionbutton.FloatingActionButton
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:src="@drawable/ic_add"
          app:backgroundTint="@color/orange"
          app:borderWidth="0dp"
          app:elevation="6dp"
          app:fabSize="normal" />
      

      这篇关于Android 更改浮动操作按钮颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆