fab.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(PrivateDealActivity.this, R.color.colorWhite)));
这是行不通的。如何更改浮动操作按钮的背景颜色
fab.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(PrivateDealActivity.this, R.color.colorWhite)));
这是行不通的。如何更改浮动操作按钮的背景颜色
默认情况下使用材质组件 FloatingActionButton
和材质主题,它使用带有属性的颜色colorSecondary
。
要更改此颜色,您有不同的选择:
app:backgroundTint
在 xml 中使用该属性:<com.google.android.material.floatingactionbutton.FloatingActionButton
...
app:backgroundTint=".." />
<item name="backgroundTint">
属性使用自定义样式 <!--<item name="floatingActionButtonStyle">@style/Widget.MaterialComponents.FloatingActionButton</item> -->
<style name="MyFloatingActionButton" parent="@style/Widget.MaterialComponents.FloatingActionButton">
<item name="backgroundTint">....</item>
</style>
materialThemeOverlay
属性仅覆盖组件的默认颜色colorSecondary
: <style name="MyFloatingActionButton" parent="@style/Widget.MaterialComponents.FloatingActionButton">
<item name="materialThemeOverlay">@style/MyFabOverlay</item>
</style>
<style name="MyFabOverlay">
<item name="colorSecondary">@color/custom2</item>
</style>
请试试这个。
app:backgroundTint="@android:color/darker_gray"
您可以使用此代码更改背景颜色:
FloatingActionButton button = findViewById(R.id.your_button);
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP && button instanceof AppCompatButton) {
((AppCompatButton) button).setSupportBackgroundTintList(ColorStateList.valueOf(*your color in integer*));
} else {
ViewCompat.setBackgroundTintList(button, ColorStateList.valueOf(*your color in integer*));
}
只需使用此属性:
android:backgroundTint="@color/yourColor"
你的代码是完全正确的。我还以类似的方式编写了以编程方式更改浮动操作按钮的背景颜色。只需检查一次您的 xml 代码。我的 XML 代码:
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
app:borderWidth="0dp"
android:id="@+id/fab"
android:layout_margin="16dp"
app:elevation="8dp"/>
请记住,保持borderWidth="0dp"
很重要。现在,在 Java 代码中:
fab = view.findViewById(R.id.fab);
fab.setBackgroundTintList(ColorStateList.valueOf(required_color));
在这里,您可以使用or替换required_color。Color.parseColor()
ContextCompat.getColor()
解决您的问题的一个简单方法是更改您的默认 colorAccent。
如Android Developers-FloatingActionButton所述:
此视图的背景颜色默认为主题的 colorAccent。如果您希望在运行时更改此设置,则可以通过 setBackgroundTintList(ColorStateList) 进行。
如果无法更改您的颜色口音,关于您的项目的要求,但您需要以编程方式实现这一点,您可以发布您的代码,我将非常乐意更新我的答案。
你可以使用
app:backgroundTint="@color/colorAccent"
像下面
<android.support.design.widget.FloatingActionButton
android:id="@+id/add_student_house"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:backgroundTint="@color/colorAccent"
android:src="@drawable/ic_house_add"/>
请注意它是app:backgroundTint不是android:backgroundTint
你也可以通过添加样式来做到这一点:
<style name="AppTheme.FloatingActionButton" >
<item name="colorAccent">@color/colorAccent</item>
</style>
并在浮动操作按钮中将此样式作为主题,如下所示:
android:theme="@style/AppTheme.FloatingActionButton"
<android.support.design.widget.FloatingActionButton
android:id="@+id/add_student_house"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.FloatingActionButton"
android:src="@drawable/ic_house_add"/>