【问题标题】:How to change toolbar color如何更改工具栏颜色
【发布时间】:2015-08-26 21:49:51
【问题描述】:

我一直在寻找如何自定义工具栏,例如如何添加背景颜色,但我不明白它是如何工作的。

我一直在尝试为我的工具栏添加自定义样式,但没有任何结果...

清单

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.Design">

style.xml 文件

<resources>

    <style name="Theme.Design" parent="Base.Theme.Design">
    </style>

    <style name="Base.Theme.Design" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/red</item>
        <item name="colorPrimaryDark">@color/red</item>
        <item name="colorAccent">@color/red</item>
        <item name="android:textColorPrimary">@color/white</item>
        <item name="android:windowActionBarOverlay">true</item>
        <item name="windowActionBarOverlay">true</item>
    </style>    
    ...

以及布局中的工具栏

<android.support.v7.widget.Toolbar
            android:id="@+id/home_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"/>

【问题讨论】:

    标签: android toolbar


    【解决方案1】:

    谢谢,但任何解决方案都有效。

     <android.support.v7.widget.Toolbar
                android:id="@+id/home_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"/>
    

    toolbar.setBackgroundColor(Color.parseColor("#80000000"));
    

    可能是因为我的工具栏在 android.support.design.widget.CoordinatorLayout 中(放置一个 android.support.design.widget.FloatingActionButton)?

    【讨论】:

    • 感谢您节省了我的时间。
    【解决方案2】:

    事实上,有一个Android Developers pro-tip 详细介绍了如何使用colorPrimary 为工具栏着色。

    您绝对是在正确的轨道上,将colorPrimary 添加到您的主题中。您需要在 Toolbar 上设置背景:

    <android.support.v7.widget.Toolbar
            android:id="@+id/home_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"/>
    

    注意,如果您有深色 colorPrimary 和浅色主题,您还需要添加 android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 以确保文本和图标在深色背景上为白色。

    【讨论】:

    • 但是我如何动态地做到这一点,因为我在整个应用程序中使用通用工具栏,有时我们想要不同的样式,特别是某个地方的同一个工具栏的渐变背景,这也应该允许我更改字体样式工具栏标题或该工具栏下的文本视图?!
    【解决方案3】:

    从资源文件中设置颜色

    toolbar.setBackgroundColor(getResources().getColor(R.color.red));
    

    【讨论】:

      【解决方案4】:

      您可以在xml中设置背景。

      <android.support.v7.widget.Toolbar
              android:id="@+id/home_toolbar"
              android:layout_width="match_parent"
              android:layout_height="?attr/actionBarSize"
              android:background="?attr/colorPrimary"
      />
      

      【讨论】:

      • 谢谢,但它不起作用。可能是因为我在 CoordinatorLayout 中?
      【解决方案5】:

      使用这个

      toolbar.setBackgroundColor((Color.parseColor("#80000000")));
      

      【讨论】:

      • 谢谢,但它不起作用。可能是因为我在 CoordinatorLayout 中?
      【解决方案6】:

      尝试使用

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

      在你的风格。它与窗口背景的标签名称相同,但仅当您将其与工具栏样式一起使用时才会更改工具栏背景颜色。

      【讨论】:

        猜你喜欢
        • 2016-05-21
        • 1970-01-01
        • 2010-12-10
        • 2021-07-19
        • 2020-05-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-21
        相关资源
        最近更新 更多