Установить цвет значка FAB
текущий FAB
Я хотел бы знать, как изменить цвет значка виджета FAB (плавающая кнопка действия), поставляемого " com.андроид.поддержка: дизайн: 22.2.0 ' библиотека от зеленого до белого.
стиль.в XML
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/color_primary</item>
<item name="colorPrimaryDark">@color/color_primary_dark</item>
<item name="colorAccent">@color/accent</item>
</style>
<color name="color_primary">#00B33C</color>
<color name="color_primary_dark">#006622</color>
<color name="accent">#FFB366</color>
activity_main.в XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include android:id="@+id/toolbar" layout="@layout/toolbar" />
<TextView android:id="@+id/text"
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:paddingTop="16dp"
android:textSize="20sp" />
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="8dp"
android:paddingBottom="16dp" />
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:src="@android:drawable/ic_input_add"
android:layout_margin="24dp"
app:elevation="6dp"
app:pressedTranslationZ="12dp"
app:borderWidth="0dp" />
5 ответов
вы можете изменить его программно с помощью 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: 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"
/>
это проще, чем получить drawables, вам нужно только получить доступ к цветовому фильтру и установить его в цвет, который вы хотите.
FloatingActionButton myFab = (FloatingActionButton) findViewById(R.id.myfabid);
myFab.setColorFilter(Color.WHITE);
используйте белую версию ic_add С сайта дизайна google.
android:tint
выглядит как чистое решение, но не поддерживается ниже уровня API 21
использование растрового изображения добавляет меньшую сложность вашему приложению, чем попытка изменить цвет существующего значка программным способом. Меньшая сложность означает меньше вещей для тестирования:)
С FloatingActionButton
выходит ImageView
можно использовать ImageViewCompat
подкрасить значок:
ImageViewCompat.setImageTintList(
floatingActionButton,
ColorStateList.valueOf(Color.WHITE)
);