简体   繁体   English

Android工具栏可以更改后退按钮颜色,但不能更改标题颜色

[英]Android ToolBar can change back button color but not title color

I want to change my Toolbar title color to blue. 我想将Toolbar标题颜色更改为蓝色。 It stays white. 它保持白色。 I was able to set the back button but not the title color - am I missing something? 我可以设置后退按钮,但不能设置标题颜色-我错过了什么吗?

I've tried to change it at 3 different places: 我尝试在3个不同的地方进行更改:

Activity 活动

    Toolbar toolbar = (Toolbar) findViewById(R.id.detail_toolbar);
    toolbar.setTitleTextColor(0xff00ff99);
    setSupportActionBar(toolbar);

activity.xml activity.xml

        <android.support.v7.widget.Toolbar
            android:id="@+id/detail_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:theme="@style/ToolBarStyle"
            android:background="@color/colorPrimaryDark"
            android:titleTextColor="@android:color/holo_red_dark"
            app:subtitleTextColor="@color/colorPrimaryDark"
            app:layout_collapseMode="pin"

            app:popupTheme="@style/ToolBarStyle" />

v21 styles.xml v21 styles.xml

    <!-- ToolBar -->
    <style name="ToolBarStyle" parent="Theme.AppCompat">

    <item name="titleTextColor">@color/colorPrimaryDark</item>
    <item name="android:titleTextColor">@color/colorPrimaryDark</item>
    <item name="android:textColorPrimary">@color/colorPrimaryDark</item>
    <item name="android:textColorSecondary">@color/colorPrimaryDark</item>
    <item name="actionMenuTextColor">@color/colorPrimaryDark</item>
    <item name="android:textColor">@color/colorPrimaryDark</item>
</style>

styles.xml styles.xml

    <style name="ToolBarStyle" parent="Theme.AppCompat">
    <item name="titleTextColor">@color/colorPrimaryDark</item>
    <item name="android:titleTextColor">@color/colorPrimaryDark</item>
    <item name="android:textColorPrimary">@color/colorPrimaryDark</item>
    <item name="android:textColorSecondary">@color/colorPrimaryDark</item>
    <item name="actionMenuTextColor">@color/colorPrimaryDark</item>
    <item name="android:textColor">@color/colorPrimaryDark</item>
</style>

What am I missing? 我想念什么? It stays white no matter what. 无论如何,它保持白色。 I've even went through the 150 references to #ffffff in the whole project and nowhere is the toolbar referenced there. 我什至在整个项目中遍历了对#ffffff的150个引用,并且那里没有引用任何工具栏。

targetSdk 26, running on Android 7.1.1 在Android 7.1.1上运行的targetSdk 26

Try to change this: 尝试更改此:

android:titleTextColor="@android:color/holo_red_dark" 机器人:titleTextColor = “@机器人:彩色/ holo_red_dark”

to: 至:

app:titleTextColor="@android:color/holo_red_dark" 应用程式:titleTextColor = “@机器人:彩色/ holo_red_dark”

in your activity.xml android.support.v7.widget.Toolbar section. 在您的activity.xml android.support.v7.widget.Toolbar部分中。

Create custom Toolbar like this: 创建自定义Toolbar如下所示:

toolbar_layout.xml toolbar_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="false"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/actionBarSize"
        android:background="@android:color/backgroundColor"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:theme="@style/Base.Theme.AppCompat.Light.DarkActionBar">

        <TextView
            android:id="@+id/toolbar_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/blue"/>

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

Include it in you layout: 将其包括在您的布局中:

<include layout="@layout/toolbar_layout"/>

Use it in your activity's onCreate method: 在您活动的onCreate方法中使用它:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_new_card);

    Toolbar toolbar = findViewById(R.id.toolbar);
    TextView toolbarTitle = findViewById(R.id.toolbar_title);

    setSupportActionBar(toolbar);
    if (getSupportActionBar() != null)
        getSupportActionBar().setDisplayShowTitleEnabled(false);

    toolbarTitle.setText("Your title");
    ...
}

Hope it will help. 希望它会有所帮助。

Ok so I have found the answer by literally stack tracing my code. 好的,所以我通过逐字地跟踪我的代码找到了答案。

I have a PagerAdapter returning different fragments. 我有一个PagerAdapter返回不同的片段。 I thought that each Activity was responsible to the Tabbar (it's a Master/Detail pattern for different tabs each) as I've had to set stuff like setSupportActionBar() which I've done in the subsequent Activities . 我认为每个Activity都对Tabbar负责(这是每个选项卡的Master / Detail模式),因为我必须设置诸如setSupportActionBar()类的东西,这是我在随后的Activities所做的。

Stacktracing my code I've realized that there is a CollapsingToolbarLayout being find by view ID. 我的代码进行了堆栈跟踪,我已经意识到有一个CollapsingToolbarLayout正在通过视图ID查找。 This is where I set the bar's title. 这是我设置栏标题的地方。 From there, I didn't find any title property but I've let auto complete lead me to setCollapsedTitleTextColor and setExpandedTitleColor . 从那里,我没有找到任何title属性,但是我让自动完成功能使我进入了setCollapsedTitleTextColorsetExpandedTitleColor

So although the getColor() part is deprecated it's 因此,尽管不赞成使用getColor()部分,

bar.setCollapsedTitleTextColor(getResources().getColor(R.color.colorPrimaryDark));
bar.setExpandedTitleColor(getResources().getColor(R.color.colorPrimaryDark));

Still no idea why all the other places I've set it at didn't do the trick but very happy it's working now. 仍然不知道为什么我设置的所有其他地方都没有成功,但很高兴它现在可以工作了。

In Layout file use the Toolbar as Child layout of AppBarLayout 在布局文件中,将工具栏用作AppBarLayout的子布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
<android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay">

        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.AppBarLayout>
</LinearLayout>

Style.xml Style.xml

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

Inside MainActivity 内部MainActivity

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       Toolbar  toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM