安卓自定义工具栏主题未生效。

3

我有一个应用程序,使用自定义的工具栏样式。以前,它能正确地应用主题,但在最近更新 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" />

请注意,现在我特别告诉ActionBar使用MyActionBarTitleText样式。

我还尝试在这里明确告诉它使用自定义主题,通过将“app:theme”更改为:

app:theme="@style/MyActionBar"

但这并没有起到帮助作用。

恐怕我无能为力。我知道基础主题可以工作,因为我可以更改背景颜色,但使用actionBarStyle项目就是不起作用。

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

1个回答

0

虽然已经很晚了,但还是回答一下。

为您的工具栏创建自定义主题并设置 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"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接