gpt4 book ai didi

android - FAB 更改可绘制图标的颜色

转载 作者:行者123 更新时间:2023-12-05 00:21:29 27 4
gpt4 key购买 nike

我在布局文件中将我的 FAB 定义为:

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/search"
app:rippleColor="@color/primary"
app:elevation="1dp"
app:borderWidth="4dp" />

这里 @drawable/search是我从 Android Studio 的 Vector Asset 工具(Asset Type Material Icon)创建的一个文件。它被创建为黑色。如何更改 FAB 定义中使用的图标的颜色?

最佳答案

使用 android:tint属性,您可以像这样设置颜色

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:tint="@android:color/white"
android:src="@android:drawable/ic_input_add"/>

您可以使用 ColorFilter. 以编程方式更改它。
//get the drawable
Drawable myFabSrc = getResources().getDrawable(android.R.drawable.ic_input_add);

//copy it in a new one
Drawable willBeWhite = myFabSrc.getConstantState().newDrawable();

//set the color filter, you can use also Mode.SRC_ATOP
willBeWhite.mutate().setColorFilter(Color.WHITE, PorterDuff.Mode.MULTIPLY);

//set it to your fab button initialized before
myFabName.setImageDrawable(willBeWhite);

关于android - FAB 更改可绘制图标的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48061048/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com