AndroidX BottomAppBar navigation icon color

被刻印的时光 ゝ 提交于 2021-01-27 04:41:50

问题


I've defined a bottom bar in a layout like that:

<com.google.android.material.bottomappbar.BottomAppBar
    android:id="@+id/bottomBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    app:navigationIcon="@drawable/ic_menu_black_24dp" />

The navigation icon is a generic icon generated from the "new vector drawable" wizard. Is there a way to apply a tint to it?

The bottom bar is not the activity action bar.

What I've tried so far:

  • applied a theme with an overridden textColorPrimary color
  • applied a theme with an overridden controlColorNormal color

If there's a solution that also works on icons supplied by loading a menu from XML (using inflateMenu() or replaceMenu()), that would be perfect.


回答1:


The navigation icon color is bases on colorControlNormal attribute.
You can override it using:

<com.google.android.material.bottomappbar.BottomAppBar
    android:theme="@style/ThemeOverlay.BottomAppBar"

with:

<style name="ThemeOverlay.BottomAppBar">
    <item name="colorControlNormal">@color/....</item>
</style>




回答2:


For programmatically changing color of the navigation icon insert into your activity 'onCreate()' this code:

override fun onCreate(savedInstanceState: Bundle?) {
   ...
   if (myIsLightTheme)
       bottomBar.navigationIcon?.setColorFilter(Color.BLACK, PorterDuff.Mode.SRC_ATOP)
   else 
       bottomBar.navigationIcon?.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP)
   ...
}


来源:https://stackoverflow.com/questions/52645566/androidx-bottomappbar-navigation-icon-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!