【问题标题】:Unable to change icon color in layout file for a FloatingActionButton on Android Studio无法更改 Android Studio 上 FloatingActionButton 布局文件中的图标颜色
【发布时间】:2020-12-15 19:13:33
【问题描述】:

所以,我有这个带有以下骨架的布局文件:

<layout>
  <CoordinatorLayout>
    <AppBarLayout>
    ... some tags and stuff ...
    </AppBarLayout>
    <NestedScrollView>
    .... nested scroll view stuff
    </NestedScrollView>
    <!-- Finally! Here comes.... -->
    <FloatingActionButton>
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="8dp"
        android:backgroundTint="@color/someDarkBlueColor"
        android:tint="@color/colorWhite"
        android:src="@drawable/ic_white_camera" />
        ... closing tags and stuff ....

ic_white_camera如下:

<vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="200dp" android:height="200dp" android:viewportWidth="24.0" android:viewportHeight="24.0">
    <path android:fillColor="@color/colorWhite" android:pathData="M12,12m-3.2,0a3.2,3.2 0,1 1,6.4 0a3.2,3.2 0,1 1,-6.4 0"/>
    <path android:fillColor="@color/colorWhite" android:pathData="M9,2L7.17,4L4,4c-1.1,0 -2,0.9 -2,2v12c0,1.1 0.9,2 2,2h16c1.1,0 2,-0.9 2,-2L22,6c0,-1.1 -0.9,-2 -2,-2h-3.17L15,2L9,2zM12,17c-2.76,0 -5,-2.24 -5,-5s2.24,-5 5,-5 5,2.24 5,5 -2.24,5 -5,5z"/>
</vector>

@color/colorWhite 是.. 你猜对了。白...

然而android坚持把相机图标渲染成黑色

我已尝试添加android:tint="@color/colorWhite"。没办法。。

添加了android:foregroundTint="@color/colorWhite" 和... Nada!

仍然得到这个:

这有什么线索吗?

向你们致以最诚挚的问候.. :-)

【问题讨论】:

  • 您是否尝试使用 app:tint="@color/colorWhite" 而不是 "android:tint"?如果你使用 com.google.android.material 包中的 FloatingActionButton,你应该添加 app:tint 而不是 android:tint
  • 完美!那钉了它。非常感谢!我想知道为什么它只是忽略了我在 ic_white_camera.xml 中添加的白色......
  • (现在...如何将此问题标记为已回答??)
  • 我添加了这个作为答案。如果您能解决问题,您可以将此标记为已回答。

标签: android android-layout floating-action-button material-components-android


【解决方案1】:

右边的attributes是:

  <com.google.android.material.floatingactionbutton.FloatingActionButton
      app:srcCompat="@drawable/ic_white_camera"
      app:tint="@color/colorWhite"
      app:backgroundTint="@color/someDarkBlueColor"/>

app:srcCompatVectorDrawable 一起工作很重要。

【讨论】:

    【解决方案2】:

    如果你使用com.google.android.material包中的FloatingActionButton,你应该添加app:tint而不是android:tint。然后在你的xml中你应该使用app:tint="@color/colorWhite"而不是使用android:tint="@color/colorWhite"

    【讨论】:

      【解决方案3】:

      我不确定你真正想要什么效果,但你可以尝试评论或删除这一行:

      android:backgroundTint="@color/someDarkBlueColor"
      

      或者调整someDarkBlueColor的值。

      【讨论】:

      • 我希望相机是白色的。 Khalana Chinthaka 为我解决了这个问题。
      猜你喜欢
      • 2011-10-06
      • 2018-11-14
      • 2017-08-25
      • 2018-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-27
      相关资源
      最近更新 更多