【问题标题】:Android custom toolbar theme not being applied未应用Android自定义工具栏主题
【发布时间】:2016-01-02 04:15:18
【问题描述】:

我有一个使用自定义工具栏样式的应用。以前,它正确应用了主题,但在最近更新 Android Studio 后,它已停止正常工作。

theme.xml 是这样定义的:

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

    <style name="OurTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">

        <item name="android:actionBarStyle">@style/MyActionBar</item>
        <item name="android:buttonStyle">@style/ButtonAppTheme</item>
        <item name="android:imageButtonStyle">@style/ImageButtonAppTheme</item>
        <item name="android:listSeparatorTextViewStyle">@style/BlueListSeparatorTextViewStyle</item>
        <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>

        <!-- colorPrimary is used for the default action bar background -->
        <item name="colorPrimary">@color/action_bar_color</item>

        <!-- colorPrimaryDark is used for the status bar -->
        <!--<item name="colorPrimaryDark">@color/my_awesome_darker_color</item>-->

        <!-- colorAccent is used as the default value for colorControlActivated
             which is used to tint widgets -->
        <item name="colorAccent">@color/btn_accent_color</item>
    </style>

    <!-- Application theme. -->
    <style name="OurTheme" parent="OurTheme.Base">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
        <item name="android:listSeparatorTextViewStyle">@style/BlueListSeparatorTextViewStyle</item>
        <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
    </style>


</resources>

我们的 styles.xml 包含:

<!-- ActionBar styles ThemeOverlay.AppCompat.ActionBar -->
    <style name="MyActionBar" parent="Widget.AppCompat.ActionBar">
        <item name="android:titleTextStyle">@style/MyActionBarTitleText</item>
    </style>

    <!-- ActionBar title text -->
    <style name="MyActionBarTitleText" parent="TextAppearance.AppCompat.Widget.ActionBar.Title">
        <item name="android:textColor">@color/actionbar_text</item>
    </style>

actionbar_text 在我们的 colors.xml 中定义:

<color name="actionbar_text">#ffffffff</color>

最后,我们的自定义工具栏布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    app:theme="@style/ThemeOverlay.AppCompat.Dark" />

以前,这很有效,我们的应用在其自定义工具栏中有白色文本。现在它在应用程序中的任何地方都默认为黑色,我发现可以更改工具栏标题文本颜色的唯一方法(除了在代码中进行)是将工具栏布局更改为:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    app:titleTextAppearance="@style/MyActionBarTitleText"
    app:theme="@style/ThemeOverlay.AppCompat.Dark" />

请注意,现在我专门告诉操作栏使用 MyActionBarTitleText 样式。

我还尝试通过将“app:theme”更改为:

app:theme="@style/MyActionBar"

但这没有帮助。

恐怕我不知所措。我知道基本主题有效,因为我可以更改背景颜色,但使用 actionBarStyle 项目只是拒绝工作。

这也影响了我们的一些自定义按钮布局的文本布局和理由,我无法弄清楚它为什么停止工作。有人有什么建议吗?

【问题讨论】:

    标签: android xml android-theme


    【解决方案1】:

    已经很晚了,但正在回答。

    为您的工具栏创建一个自定义主题并设置android:background

    <style name="ToolbarStyle" parent="Widget.AppCompat.Toolbar">
           <item name="android:background">@color/blue</item> // change your color here and add other attributes
    </style>
    

    现在将此主题应用到 XML 中的工具栏,例如:

    style="@style/ToolbarStyle"
    

    【讨论】:

      猜你喜欢
      • 2015-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多