为什么工具栏颜色停留在白色上?

时间:2018-09-16 10:44:18

标签: android android-toolbar

我已经实现了官方文档中提到的导航抽屉,这是我的代码:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

<LinearLayout
    android:id="@+id/content_linear"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar" />

    <include layout="@layout/content_main" />

</LinearLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:menu="@menu/drawer_view"
    app:headerLayout="@layout/nav_header"/>

</android.support.v4.widget.DrawerLayout>

styles.xml

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="titleTextColor">@color/colorWhite</item>
    <item name="android:windowTranslucentStatus">true</item>
</style>

<style name="MyButton" parent="Theme.AppCompat.Light">
    <item name="colorControlHighlight">@color/colorAccent</item>
    <item name="colorButtonNormal">@color/colorButton</item>
</style>

<style name="AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

</resources>

我还在v-21文件夹中创建了styles.xml,并在建议的相关问题上添加了“ android”前缀作为其他答案,但这还是行不通的。工具栏的颜色仍为白色。这是它的外观:

enter image description here

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sampleapp.drawer">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar">

    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

activity_main.xml的更新的工具栏部分

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

更新的清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sampleapp.drawer">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

更新了styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:colorPrimary">@color/colorPrimary</item>
    <item name="android:colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="android:colorAccent">@color/colorAccent</item>
    <item name="titleTextColor">@color/colorWhite</item>
    <item name="android:windowTranslucentStatus">true</item>
</style>

1 个答案:

答案 0 :(得分:1)

清单中设置的活动主题错误。

在您的<application>标签中,更改

android:theme="@style/Theme.AppCompat.Light.NoActionBar"

android:theme="@style/AppTheme"

并从您的theme标记中删除<activity>字段。