【问题标题】:Change Toolbar background color programmatically does not change Toolbar Title Background color以编程方式更改工具栏背景颜色不会更改工具栏标题背景颜色
【发布时间】:2020-06-18 18:53:35
【问题描述】:

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

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

结果如下:

之前:

之后:

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

这是我的工具栏 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>

【问题讨论】:

  • 您是否在styles.xml中为actionbar应用了任何自定义样式,可能其中一些是冲突的。
  • 请发布您的布局 xml
  • @DavidJhons 我已经发布了有关问题的新细节

标签: android android-actionbar android-toolbar


【解决方案1】:

如下修改你的代码:

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>

结果

设置新背景色之前:

之后:

【讨论】:

  • 您将 colorPrimary/PrimaryDark 等放在哪里,即您将“AppTheme”放在哪里?另外,您没有设置父主题是否有任何特殊原因?
  • 它基本上只是一种自定义样式,我不想从其他主题继承任何属性,因此我没有指定父主题。关于colorPrimary:这是在应用程序/活动使用的 AppTheme 中设置的。默认情况下,Toolbar 根据colorPrimary 属性设置背景,除非您不使用样式覆盖它。
【解决方案2】:

迟到了,但我希望它会有所帮助

我的 Activity 样式中有这个 item

<item name="android:background">someColor</item>

所以当我更改toolbar 颜色时,标题和菜单项并没有改变背景。 我刚刚删除了这个item,现在它可以完美运行了。

我没有时间了解细节,但我认为它可能对其他人有用。

【讨论】:

  • 我发现删除它确实修复了工具栏。但是,如何在不增加工具栏的情况下设置背景颜色?
【解决方案3】:

使用它来访问文本视图

public void changeToggleTitle() {
    if (mToolbar != null) {
        for(int i= 0; i < mToolbar.getChildCount(); i++){
            View v = mToolbar.getChildAt(i);
            if(v != null && v instanceof TextView){
                TextView t = (TextView) v;
                // Do the magic 
            }
        }
    }
}

【讨论】:

    【解决方案4】:

    在我的style 我改变了

            <item name="android:background">@color/background</item>
    

            <item name="android:windowBackground">@color/background</item>
    

    这似乎已经为我解决了。

    【讨论】:

      【解决方案5】:
      mToolbar.setBackgroundResource(mGameEnum.getPrimeColorRes());
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-03-31
        • 1970-01-01
        • 1970-01-01
        • 2010-11-19
        • 2016-04-21
        • 1970-01-01
        • 1970-01-01
        • 2021-04-13
        相关资源
        最近更新 更多