Android工具栏颜色未由colorPrimary设置

Sia*_*ash 5 android

根据谷歌文档,我应该能够在主题中使用colorPrimary设置工具栏背景的颜色,但它不起作用.这就是我所拥有的:

styles.xml:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/light_purple</item>
        <!-- colorPrimaryDark is used for the status bar -->
        <item name="colorPrimaryDark">@color/dark_purple</item>
        <!-- colorAccent is used as the default value for colorControlActivated,
             which is used to tint widgets -->
        <item name="colorAccent">@color/dark_purple</item>

        <item name="colorSwitchThumbNormal">@color/light_purple</item>
    </style>

</resources>
Run Code Online (Sandbox Code Playgroud)

活动布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/AppTheme"
        tools:showIn="@layout/activity_main">
        <TextView
            android:id="@+id/pivot_title_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="toolbar text view" />
    </android.support.v7.widget.Toolbar>
...
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

活动:

public class MainActivity extends AppCompatActivity {

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    }
Run Code Online (Sandbox Code Playgroud)

我在清单中将我的应用主题设置为AppTheme:android:theme="@style/AppTheme" > 我在build.gradle中设置了android支持appcompat

compile 'com.android.support:appcompat-v7:22.1.0'
Run Code Online (Sandbox Code Playgroud)

但是我的工具栏仍然没有着色. 我知道我可以在布局文件中手动设置工具栏背景颜色,但不应该从主题中获取颜色吗?正如你所看到的强调颜色正在起作用.

在此输入图像描述

Gab*_*tti 6

你可以为你的风格应用一种风格 Toolbar

风格和主题是不同的.

样式是工具栏视图的本地样式,例如背景颜色.

对于工具栏中充气的所有ui元素,主题是全局的,例如标题和图标的颜色.

更多信息在这里.

<android.support.v7.widget.Toolbar
       style="@style/HeaderBar"/>
Run Code Online (Sandbox Code Playgroud)

哪里:

<style name="HeaderBar">
    <item name="android:background">?colorPrimary</item>
</style>
Run Code Online (Sandbox Code Playgroud)

否则,您可以定义工具栏的背景.

<android.support.v7.widget.Toolbar 
  android:background="?attr/colorPrimary">
Run Code Online (Sandbox Code Playgroud)


ch3*_*anz 5

工具栏不会从您的主题获得原色。您必须设置工具栏的以下xml属性

android:background="@color/primary"
Run Code Online (Sandbox Code Playgroud)

这是我工具栏的工作实现。

    <?xml version="1.0" encoding="utf-8"?>
    <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/primary"
        android:minHeight="?actionBarSize">
</android.support.v7.widget.Toolbar>
Run Code Online (Sandbox Code Playgroud)

希望它也对您有用。