使用AppCompat时,样式工具栏无法正常工作

3
我正在尽力为我的应用程序设计ActionBar(即android.support.v7.widget.Toolbar),但它就是无法工作。在实现这一目标时,我遵循的是官方文档我真正想做的是将标题颜色变为白色。 目前它显示为黑色。我的目标API是11。
我做错了什么?

res/values/themes.xml

<resources>

    <!-- Base theme applied no matter what API -->
    <style name="MMTheme.Base" parent="Theme.AppCompat.Light">
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>

        <item name="android:textColor">@color/mm_dark_gray</item>
        <item name="colorPrimary">@color/mm_brown</item>
        <item name="colorPrimaryDark">@color/mm_dark_gray</item>
        <item name="colorAccent">@color/mm_green</item>

        <item name="android:actionBarStyle">@style/MMActionBar</item>
        <item name="android:actionBarTabTextStyle">@style/MMActionBarTabText</item>
        <item name="android:actionMenuTextColor">@color/mm_green</item>

        <!-- Support library compatibility -->
        <item name="actionBarStyle">@style/MMActionBar</item>
        <item name="actionBarTabTextStyle">@style/MMActionBarTabText</item>
        <item name="actionMenuTextColor">@color/mm_green</item>
    </style>

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

    <!-- ActionBar style -->
    <style name="MMActionBar" parent="@style/Widget.AppCompat.ActionBar">
        <item name="android:background">@color/mm_green</item>

        <item name="android:titleTextStyle">@style/MMActionBarTitleText</item>

        <!-- Support library compatibility -->
        <item name="titleTextStyle">@style/MMActionBarTitleText</item>
    </style>

    <!-- ActionBar title text -->
    <style name="MMActionBarTitleText" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
        <item name="android:textColor">@color/mm_white</item>
        <!-- The textColor property is backward compatible with the Support Library -->
    </style>

    <!-- ActionBar tabs text -->
    <style name="MMActionBarTabText" parent="@style/Widget.AppCompat.ActionBar.TabText">
        <item name="android:textColor">@color/mm_green</item>
        <!-- The textColor property is backward compatible with the Support Library -->
    </style>

</resources>

膨胀的 android.support.v7.widget.Toolbar

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mm_toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"/>

Activity
// Setup main Toolbar
Toolbar toolbar = (Toolbar) mInflatedActivity.findViewById(R.id.mm_toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setIcon(R.drawable.ic_launcher);

我已经尝试了所有我能想到的方法,并在网上搜索了很多,但是我无法使它工作。我错过了什么吗?
当前的结果是(不要在意搜索图标):

enter image description here

android.support.v7.widget.Toolbar的xml属性中添加style="@style/MMActionBar"会产生以下结果:

enter image description here

将主要父题材更改为Theme.AppCompat.Light.DarkActionBar没有任何效果。

1
创建一个工具栏布局,然后将其交给您的工具栏。 - Nouman Ghaffar
3个回答

4
只需在您的布局xml中使用此代码。
<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/red600"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

将主题设置为ThemeOverlay.AppCompat.Dark.ActionBar,这样会使标题文本变为白色。

app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"

我猜这是正确的主题工具栏的方法,因为官方Android开发者博客如此说。

Styling of Toolbar is done differently to the standard action bar, and is set directly onto the view.

Here's a basic style you should be using when you're using a Toolbar as your action bar:

<android.support.v7.widget.Toolbar  
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    app:theme="@style/ThemeOverlay.AppCompat.ActionBar" />

The app:theme declaration will make sure that your text and items are using solid colors (i.e 100% opacity white).


保持 style="@style/MMActionBar" 的同时进行操作可以提供所需的颜色。这真的是应该这样做的方式吗?我参考的官方文档使用 <item name="android:actionBarStyle"></item> 等等,但似乎没有任何作用。为什么这不起作用呢? - Krøllebølle
通常我无法在使用Toolbar时使android:actionBarStyle属性起作用,所以我转而使用这种方法,而且这种方式似乎非常普遍。只需查看官方的Android开发者博客,他们也在使用相同的技术。因此,我想这是正确的做法。http://android-developers.blogspot.in/2014/10/appcompat-v21-material-design-for-pre.html - capt.swag
@OP 我猜,如果我的解决方案解决了你的问题,你应该点赞并将答案标记为已解决。 - capt.swag

1
我曾经遇到过同样的问题。以下解决方案解决了我的问题。 为此,您需要在两个地方进行更改。
1.像这样修改您的工具栏xml,
<android.support.v7.widget.Toolbar
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="@color/ColorPrimary"
android:elevation="2dp"
xmlns:android="http://schemas.android.com/apk/res/android" >
<TextView
    android:id="@+id/TV_tool_bar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="My App Name"
    android:textSize="18sp"
    android:textColor="#FFFFFF"/>
</android.support.v7.widget.Toolbar>

在你的activity文件中进行以下修改:
Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(null);

就是这样。这将使您的标题颜色变为白色。干杯..!


1
这也是一个可行的解决方案,虽然 Toolbar 标题被移除并且我们自己处理。如果您想要的不是黑色或白色或主题给出的任何颜色,似乎更容易实现。 - Krøllebølle

0
使用此功能可更改工具栏上字母的颜色:
<android.support.v7.widget.Toolbar      
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/ColorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
android:elevation="4dp"
>

</android.support.v7.widget.Toolbar>

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