工具栏颜色在 android 中没有改变

ToolBar color not changing in android

我在我的 activity 中添加了独立的工具栏,它显示了但没有显示我为它定义的颜色。我使用的代码有什么问题?

下面是我使用的代码。

activity_categories.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:weightSum="10"
    android:background="#D32F2F"
    tools:context="com.example.knowledgeup.LoginActivity" >

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_height="0dp"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:elevation="4dp"
        style="@style/ToolbarTheme" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="vertical"
        android:layout_weight="8" >

        <ListView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </ListView>

    </LinearLayout>   

</LinearLayout>

这就是我将它添加到 java 文件 activity 中的方式

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_categories);

     mToolbar = (Toolbar)findViewById( R.id.toolbar );
     mToolbar.setTitle(R.string.title_activity_categories);

}

这是我的styles.xml

<resources>

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="Theme.AppCompat.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
        <item name="android:windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
    </style>

    <style name="ToolbarTheme" parent="Theme.AppCompat">
        <item name="colorPrimary">@color/material_blue_grey_900</item>
        <item name="colorPrimaryDark">@color/color_toolbar</item>
    </style>
</resources>

这是它的显示方式

试试这个

    <android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_height="0dp"
    android:layout_width="match_parent"
    android:layout_weight="1"
    android:elevation="4dp"
    android:background = "@color/color_toolbar"
    style="@style/ToolbarTheme" />

在你的activity_categories.xml

或者在你的styles.xml

    <style name="ToolbarTheme" parent="Theme.AppCompat">
    <item name="colorPrimary">@color/material_blue_grey_900</item>
    <item name="colorPrimaryDark">@color/color_toolbar</item>
    <item name="android:background">@color/color_toolbar</item>
</style>

使用以下属性更改工具栏颜色。

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_my_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

您只需在工具栏中设置背景即可。

e.g.Code 片段

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/action_bar_bg"
    android:elevation="@dimen/elevation_button_default"
    android:theme="@style/ThemeOverlay.AppCompat.Dark" />