Android工具栏颜色未由colorPrimary

时间:2015-04-24 02:12:58

标签: 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>

活动布局:

<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>

的活动:

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);
    }

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

compile 'com.android.support:appcompat-v7:22.1.0'

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

enter image description here

3 个答案:

答案 0 :(得分:6)

您可以将样式应用于Toolbar

风格和主题不同。

样式本地到工具栏视图,例如背景颜色。

主题是全局,而不是工具栏中充斥的所有ui元素,例如标题和图标的颜色。

更多信息here

<android.support.v7.widget.Toolbar
       style="@style/HeaderBar"/>

其中:

<style name="HeaderBar">
    <item name="android:background">?colorPrimary</item>
</style>

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

<android.support.v7.widget.Toolbar 
  android:background="?attr/colorPrimary">

答案 1 :(得分:5)

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

android:background="@color/primary"

这是我工作的工具栏。

    <?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>

希望它也适合你。

答案 2 :(得分:0)

尝试将背景颜色设置为工具栏

这对我有用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar_main"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"
        android:background="@color/primary_color"
        android:gravity="right"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <ProgressBar
            android:id="@+id/pb_loading_news"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:visibility="gone" />
    </android.support.v7.widget.Toolbar>