colorPrimary未设置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'

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

在此处输入图片说明

ch3tanz

工具栏不会从您的主题获得原色。您必须设置工具栏的以下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>

希望它也对您有用。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章