自定义工具栏更改状态栏

时间:2017-03-17 15:23:59

标签: android xml android-toolbar

我正在实现我自己的工具栏/操作栏但是,状态栏颜色变为棕色,我正试图给它dafaults,这是一个深蓝色。我怎么能改变这个。我正在尝试使用style.xml主题,还有工具栏主题。但我无法做到对。任何帮助赞赏。 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>
    </style>

</resources>

toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/my_toolbar"
    android:popupTheme="@style/ThemeOverlay.AppCompat"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/colorPrimary"
    android:elevation="4dp"
    android:paddingBottom="5dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    tools:context="com.example.com.mytoolbar.MainActivity" />

menu.xml文件

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <!-- "Mark Favorite", should appear as action button if possible -->
    <item
        android:id="@+id/refresh"
        android:icon="@drawable/ic_refresh_black_48dp"
        android:title="@string/refresh"
        app:showAsAction="ifRoom"/>

    <!-- Settings, should always be in the overflow -->
    <item android:id="@+id/settings"
        android:title="@string/settings"
        app:showAsAction="never"/>

</menu>

activity_main.xml中

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.com.mytoolbar.MainActivity">

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

使用以下工具栏代码:

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/my_toolbar"
    android:popupTheme="@style/ThemeOverlay.AppCompat"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"               <!-- Check this line --> 
    android:theme="@style/MyToolbarStyle"                 <!-- Custom theme added -->  
    android:elevation="4dp" 
    android:paddingBottom="5dp"
    tools:context="com.example.com.mytoolbar.MainActivity" />

删除了android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"

如果您使用android:background="?attr/colorPrimary"工具栏将从基本应用主题获得默认colorPrimary

收件人,更改文字和溢出图标颜色,将此自定义主题添加到style.xml并应用于工具栏:

<style name="MyToolbarStyle" parent="Widget.AppCompat.Light.ActionBar">
    <item name="android:textColor">@android:color/white</item>
    <item name="android:textColorPrimary">@android:color/white</item>
    <item name="android:textColorSecondary">@android:color/white</item>
</style>

希望这有帮助。