2017-03-17 139 views
-1

但是,我正在实现自己的工具栏/操作栏,状态栏颜色变为棕色,并且我试图给它一个深蓝色的默认值。我该如何改变这一点。我试图玩弄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

尝试从toolbar.xml中删除popupTheme –

回答

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> 

希望这有助于。

+0

ok,但现在我的文本和溢出图标变成黑色。我如何将它们更改为主题默认值..白色!感谢您的帮助 – miatech

+0

请检查我的更新答案。 – tahsinRupam

+0

很好..全白主题!伟大的 – miatech