Android getColor(…)返回-1
我试图通过当前显示片段的主题获取colorPrimartVariant颜色,并将状态栏颜色设置为该颜色。为此,我编写了下面的代码块Android getColor(…)返回-1,android,colors,statusbar,Android,Colors,Statusbar,我试图通过当前显示片段的主题获取colorPrimartVariant颜色,并将状态栏颜色设置为该颜色。为此,我编写了下面的代码块 addOnDestinationChangedListener { controller, destination, arguments -> showBottomNavigationAndFabByDestinationId(destination.id) val fragment = hostFragment
addOnDestinationChangedListener { controller, destination, arguments ->
showBottomNavigationAndFabByDestinationId(destination.id)
val fragment = hostFragment?.childFragmentManager?.fragments?.first()
fragment?.context?.let { fragmentContext ->
val defaultColor = ResourcesCompat.getColor(resources,R.color.white,fragmentContext.theme)
val color = MaterialColors.getColor(fragmentContext,R.attr.colorPrimaryVariant,defaultColor)
Log.e("TAG", "addOnDestinationChangedListenerToAppContent: def: $defaultColor color : $color", )
window.statusBarColor = color
}
}
然而奇怪的是,ResourcesCompat.getColor(…)和MaterialColors.getColor(…)对每个片段都返回-1,这样状态栏的颜色就不会改变。
为什么会这样?有什么解决办法吗?谢谢