使用styles.xml中的AppCompat为android.support.v7.widget.Toolbar设置样



我在styles.xml文件的CoordinatorLayout/AppBarLayout中设置工具栏样式时遇到问题,同时使用23.1.1支持库中的AppCompat。

我想解决的问题:我的活动来自不同的应用程序项目,所有这些项目都使用Theme.AppCompat.Light.NoActionBar。有些应用程序有浅色原色,一切都很好。然而,有些应用程序具有深色原色,这需要在活动布局文件的工具栏上设置android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"——我无法做到这一点,因为其他应用程序也使用相同的代码和资源文件。

编辑:我想我现在已经解决了。保持styles.xml的原样,在"活动"中,我现在继承actionBarTheme如下http://developer.android.com/reference/android/R.styleable.html#Theme_toolbarStyle):

<android.support.v7.widget.Toolbar
    android:id="@+id/webview_toolbar"
    android:fitsSystemWindows="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="?attr/actionBarTheme"/>

请参阅以下相关问题和博客文章:

  • 如何将appcompat-v7工具栏设置为类似Theme.appcompat.Light.DarkActionBar的样式
  • https://chris.banes.me/2014/11/12/theme-vs-style/
  • 如何将appcompat-v7工具栏设置为类似Theme.appcompat.Light.DarkActionBar的样式
  • 什么时候应该使用Theme.AppCompat与ThemeOverlay.AppCompant

在阅读了以上所有内容后,我试图通过在那些具有深色原色的应用程序中从Theme.AppCompat.Light.DarkActionBar(而不是*.NoActionBar)派生应用程序主题来解决我的问题,并应用所有让动作栏消失的样式。出于绝望,我甚至尝试设置actionBarTheme和actionBarStyle。

我的应用程序有一个styles.xml文件,如下所示:

<style name="MyTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimaryDark">@color/myDarkRed</item>
    <item name="colorPrimary">@color/myRed</item>
    <item name="colorAccent">@color/myOrange</item>
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="actionBarTheme">@style/ThemeOverlay.AppCompat.Dark.ActionBar</item>
    <item name="actionBarStyle">@style/ThemeOverlay.AppCompat.Dark.ActionBar</item>
</style>

我的活动(源自AppCompatActivity)有一个layout.xml,它看起来像这样:

<android.support.v7.widget.Toolbar
    android:id="@+id/webview_toolbar"
    android:fitsSystemWindows="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

不幸的是,我的"活动"工具栏上仍然有深色文本。我做错了什么?

这一款适合我

<style name="MyToolbarTheme">
        <!-- Used to tint the back arrow, menu and spinner arrow -->
        <item name="colorPrimary">#ffffff</item>
        <item name="colorPrimaryDark">#ffffff</item>
        <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
        <item name="colorControlNormal">#ff0000</item>
        <item name="android:textColor">#ffffff</item>
        <item name="android:textColorHighlight">#ffffff</item>
        <item name="actionOverflowMenuStyle">@style/CMOptionsMenu</item>
        <item name="searchViewStyle">@style/MySearchViewStyle</item>
    </style>
    <style name="CMOptionsMenu" parent="Widget.AppCompat.PopupMenu.Overflow">
        <item name="android:popupBackground">#ffffff</item>
    </style>
    <style name="MySearchViewStyle" parent="Widget.AppCompat.SearchView">
        <item name="android:textColor">@android:color/black</item>
    </style>

您可以在工具栏上设置自己的通用样式

android:theme="@style/myToolBarLightOrDark"

然后在styles.xml集合中从Light或Dark ThemeOverlay ActionBar继承它。

最新更新