gpt4 book ai didi

android - 以编程方式更改工具栏背景颜色不会更改工具栏标题背景颜色

转载 作者:IT老高 更新时间:2023-10-28 22:26:32 26 4
gpt4 key购买 nike

我正在尝试通过这样做以编程方式更改工具栏背景颜色:

getSupportActionBar().setBackgroundDrawable(newColorDrawable(getResources().getColor(R.color.test_color_blue)));

结果如下:

之前:

enter image description here

之后:

enter image description here

工具栏标题仍然具有与以前相同的背景颜色。

这是我的工具栏 xml:

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/Theme.Toolbar">

这是主题:

<style name="Theme.Toolbar" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
<item name="android:maxHeight">@dimen/abc_action_bar_default_height_material</item>
<item name="android:background">@color/primary</item>
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/primary_dark</item>
<item name="colorAccent">@android:color/white</item>
<item name="titleTextAppearance">@style/Theme.Toolbar.Title</item>
</style>

最佳答案

如下修改你的代码:

toolbar.xml

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/MyToolbarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">

主题/风格

<style name="MyToolbarStyle">
<item name="android:maxHeight">@dimen/abc_action_bar_default_height_material</item>
<item name="android:background">@color/primary</item>
<item name="popupTheme">@style/ThemeOverlay.AppCompat.Light</item>
<item name="titleTextAppearance">@style/Theme.Toolbar.Title</item>
<!-- No need for colorPrimary, colorPrimaryDark, colorAccent here
this should go to the AppTheme -->
</style>

结果

设置新背景色之前:

picture before background color change

之后:

picture after background color change

关于android - 以编程方式更改工具栏背景颜色不会更改工具栏标题背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29464078/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com