如何以编程方式获取工具栏背景颜色?

How to get Toolbar background color programmatically?

我在整个应用程序中使用 v7 工具栏 (android.support.v7.widget.Toolbar)。设置背景颜色有一个简单的方法:

mToolbar.setBackgroundColor(Color.BLACK); // works!

但是没有获取背景颜色的方法:

mToolbar.getBackgroundColor(); // Cannot Resolve Method

我想获取工具栏的背景颜色,以便检查工具栏是否为白色。如果是白色,我想将菜单图标的颜色(默认为白色)更改为黑色。

如果我们可以假设我已经使用 setBackgroundColor() 以编程方式设置了它,那么获取工具栏背景颜色的最简单方法是什么?

int color = ((ColorDrawable) mToolbar.getBackground()).getColor();
// do your stuff