简体   繁体   中英

Why won't the text color in the app bar change?

the color of the text and icons in the app bar continue to be white, even though I'm trying to set them to be brown. What's wrong with my styles?

<style name="HobbesActionBarStyle" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:background">@color/HobbesBackground</item>
    <item name="android:tint">@color/HobbesText</item>
    <item name="colorControlNormal">@color/HobbesText</item>
    <item name="android:subtitleTextStyle">@style/Hobbes.ActionBar.CustomTitle</item>
    <item name="android:titleTextStyle">@style/Hobbes.ActionBar.CustomTitle</item>
    <item name="subtitleTextStyle">@style/Hobbes.ActionBar.CustomTitle</item>
    <item name="titleTextStyle">@style/Hobbes.ActionBar.CustomTitle</item>
</style>


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

<style name="HobbesAppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:actionBarStyle">@style/HobbesActionBarStyle</item>
    <item name="actionModeBackground">@color/HobbesBackground</item>
    <item name="colorPrimary">@color/HobbesBackground</item>
    <item name="colorPrimaryDark">@color/black_overlay</item>
    <item name="colorControlNormal">@color/HobbesBackground</item>
    <item name="colorControlActivated">@color/HobbesBackground</item>
    <item name="colorControlHighlight">@color/HobbesBackground</item>
    <item name="colorAccent">@color/HobbesText</item>

</style>

Create custom Toolbar like this:

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/yourCustomColor"/>

    </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:

@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.

You have to add textColorPrimary in style

  <item name="android:textColorPrimary">@color/colorRed</item> 

 <style name="appBarTheme" parent="Theme.AppCompat.NoActionBar"> <item name="android:textColorPrimary">@color/colorRed</item> </style> 

This is how I did it :

in xml :

     <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:drawable/dark_header">


        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            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"
                android:baselineAligned="false"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

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

</RelativeLayout>

In styles.xml :

 <style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

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

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

In activity.xml

 <include
    android:id="@+id/header"
    layout="@layout/common_header"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

In Activity :

private void setToolBar() {
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    ActionBar actionBar = getSupportActionBar();
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    final Drawable upArrow = getApplicationContext().getResources().getDrawable(R.drawable.abc_ic_ab_back_material); // this adds the back button remove it if you dont want it
    actionBar.setHomeAsUpIndicator(upArrow);
    actionBar.setDisplayShowTitleEnabled(true);
    toolbar.setTitleTextColor(getColor(R.color.colorAccent));  // this changes the text color
    actionBar.setTitle("title name);
    context = getApplicationContext();
}

hope it helps :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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