如何使用XML更改Android工具栏背景色而无需创建新的工具栏布局



所以我已经为应用程序创建了一个主题:

<style name="MainApplicationTheme" parent="@style/Theme.AppCompat">
<item name="android:background">@color/light_green</item>
</style>

,现在工具栏的颜色与主应用程序背景相同。我如何改变工具栏的颜色,而不创建工具栏的新布局,而不需要在每个单独的活动做这些代码的变化?

在你想要改变工具栏颜色的Activity中,你可以使用:

ActionBar actionBar = getSupportActionBar();
ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#000000"));
actionBar.setBackgroundDrawable(colorDrawable);

这将改变背景颜色为black。或者如果你想在你的主题中改变它,像这样做:

<style name="MainApplicationTheme" parent="@style/Theme.AppCompat">
<item name="android:background">@color/light_green</item>
<item name="actionBarTheme">@style/MyActionBar</item>
</style>
<style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">@color/black</item>
</style>

这也会把它变成黑色。

最新更新