【问题标题】:Color specific items of Android Toolbar with styles.xml使用 styles.xml 为 Android 工具栏的特定项目着色
【发布时间】:2017-02-23 19:21:20
【问题描述】:

我遇到了以下问题:我正在尝试实现像 instagram 这样的应用设计,其中大多数应用都使用浅色主题。但是,我还没有找到将工具栏文本和图标元素(如导航抽屉和后退按钮黑色)着色的方法。

当使用下面的style.xml时

<resources>
<!-- Base application theme. -->
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
     <item name="colorPrimary">#FAFAFA</item>
     <item name="colorPrimaryDark">#BDBDBD</item>
     <item name="colorAccent">#FF4081</item>
</style>

<style name="AppTheme" parent="AppTheme.Base"></style>

<style name="ToolbarTheme" parent="ThemeOverlay.AppCompat.ActionBar">
    <!-- Used to for the title of the Toolbar -->
    <item name="android:textColorPrimary">@color/black</item>
    <!-- Used to for the title of the Toolbar when parent is Theme.AppCompat.Light -->
    <item name="android:textColorPrimaryInverse">@color/black</item>
    <!-- Used to color the text of the action menu icons -->
    <item name="android:textColorSecondary">@color/black</item>
    <!-- Used to color the overflow menu icon -->
    <item name="actionMenuTextColor">@color/black</item>
</style>
</resources>

工具栏的这个属性:

android:theme="@style/ToolbarTheme"

它产生以下设计:

如您所见,导航抽屉是白色的,标签标题也是白色的。

我也尝试过这种风格,它可以正常工作,但前提是你没有像这样在你的活动中明确设置工具栏:

活动代码:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

样式.xml:

<!-- Base application theme. -->
<style name="AppTheme.Base" parent="Theme.AppCompat.Light">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

当使用这种没有明确工具栏的样式时,设计看起来像我想要的,但是,我不能以这种方式调用工具栏。如果使用 Theme.AppCompat.Light 设置工具栏,则会抛出错误。

还要注意两种样式的区别:Theme.AppCompat.Light vs Theme.AppCompat.Light.NoActionBar

关于如何做到这一点的任何想法?

【问题讨论】:

    标签: android xml android-toolbar android-styles


    【解决方案1】:

    我现在只使用标准的 android 主题,如下所示:

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/container"
        android:theme="@style/ThemeOverlay.AppCompat.Light">
    
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:titleTextColor="@color/black"/>
    </android.support.design.widget.AppBarLayout>
    

    这比尝试设置自己的风格要容易得多。

    【讨论】:

      猜你喜欢
      • 2015-03-28
      • 1970-01-01
      • 2013-09-15
      • 1970-01-01
      • 2015-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多