Ály*_*dre 18 android android-layout androidx
以下是我正在使用的代码.我正在使用androidx.每个FAB都有一个黑色图标,即使它有白色.
mylayout.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<include layout="@layout/content_geral" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="24dp"
app:backgroundTint="@color/colorPrimary"
app:srcCompat="@drawable/ic_cloud_upload"
tools:ignore="VectorDrawableCompat" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)
style.xml
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>
Run Code Online (Sandbox Code Playgroud)
Dav*_*vis 48
如果您使用的是AndroidX,要更改图标的颜色,您必须使用app:tint相反的颜色android:tint
<com.google.android.material.floatingactionbutton.FloatingActionButton
style="@style/Widget.MaterialComponents.FloatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
app:backgroundTint="@color/colorPrimary"
app:tint="@color/white"
app:srcCompat="@drawable/ic_add"
/>
Run Code Online (Sandbox Code Playgroud)
Who*_*bel 17
我有一个带有多种颜色的图标(矢量)(附件)但是我不能使用app:tint ="@ color/white"因为我的图标的颜色变为单色,如白色,我不需要这个.
所以我用set setting app修复了我的问题:tint为null:
app:tint="@null"
Run Code Online (Sandbox Code Playgroud)
我的图标(SVG):
FloatingActionButtonAndroidX 中的类使用colorOnSecondarytheme 属性为其图标着色。
如果你跟着MaterialComponents下到基本定义主题的定义,你会看到,默认值colorOnSecondary是design_default_color_on_secondary...并且是被定义为#000000。
您可以通过将app:tint属性直接添加到您的 FloatingActionButton 或通过@color/colorOnSecondary在您的主题中重新定义为您想要的任何内容来解决此问题。
您正在更改FAB 的背景颜色,而不是图标颜色。要更改图标颜色,请使用:
android:tint
Run Code Online (Sandbox Code Playgroud)
更新
您还可以通过编程方式更改颜色:
Drawable myFabSrc = getResources().getDrawable(android.R.drawable.ic_input_add);
Drawable willBeWhite = myFabSrc.getConstantState().newDrawable();
willBeWhite.mutate().setColorFilter(Color.WHITE, PorterDuff.Mode.MULTIPLY);
myFabName.setImageDrawable(willBeWhite);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3606 次 |
| 最近记录: |