You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Material3 ActionBar无法通过代码设置背景颜色的问题求助

Material3 ActionBar无法通过代码设置背景颜色的问题求助

大家好,我现在碰到个头疼的问题:想在某个特定Activity里通过代码给ActionBar设置自定义背景色,可不管怎么操作,ActionBar都没反应,甚至我调用了setBackgroundDrawable(null)也没用。有没有大佬能给点思路?谢谢啦!

下面是我的相关代码:

样式代码

<style name="Theme.App" parent="Theme.Material3.DayNight">
    <item name="toolbarStyle">@style/ToolbarStyle</item>
</style>

<style name="ToolbarStyle" parent="Widget.Material3.Toolbar">
    <item name="android:background">@color/material3_primary</item>
    <item name="titleTextColor">@color/white</item>
    <item name="navigationIcon">@drawable/ic_nav_back_dark_theme</item>
    <item name="theme">@style/ToolbarTheme</item>
</style>

<style name="ToolbarTheme">
    <item name="colorControlNormal">@color/white</item>
</style>

Activity代码

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    title = "Temp"
    supportActionBar?.setBackgroundDrawable(ColorDrawable(Color.parseColor("#0000ff"))) //it doesn't work
}

备注:内容来源于stack exchange,提问作者Konstantin Konopko

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.21 08:03:09