无法更改工具栏文本颜色

时间:2016-05-21 16:49:15

标签: java android xml android-actionbar toolbar

我想确保涵盖与此问题相关的所有版本,但我想更改工具栏的文本颜色。目前它是黑色的,我希望将其改为白色。我知道对API有不同的处理方法。 21,和API 23。

这是我的主题:

<style name="AppTheme.NoActionBar" parent="Theme.AppCompat.Light">
    <item name="android:radioButtonStyle">@style/Theme.AppCompat.DayNight</item>
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:textColor">@color/black</item>
    <item name="android:textColorPrimary">@color/black</item>
    <item name="android:textColorSecondary">@color/white</item>

    <item name="android:actionBarStyle">@style/MyTheme.ActionBarStyle</item>
    <item name="colorControlActivated">@android:color/holo_red_light</item>
    <item name="colorControlNormal">@android:color/black</item>
    <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>

</style>


<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
    <item name="color">@android:color/white</item>
</style>

<style name="MyTheme.ActionBarStyle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="android:titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
</style>

<style name="MyTheme.ActionBar.TitleTextStyle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="colorAccent">@color/white</item>
    <item name="android:textColorPrimary">@color/white</item>
</style>

这是我的工具栏xml:

<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_test"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize"
android:theme="@style/MyTheme"
android:background="@color/actionRed">

活动XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <include
        android:id="@+id/tool_bar"
        layout="@layout/toolbar_one" />

        <android.support.v4.view.ViewPager
            android:id="@+id/home_polls_viewpager_fragment_container"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

        </android.support.v4.view.ViewPager>

</LinearLayout>

1 个答案:

答案 0 :(得分:1)

For changing toolbar text color, you can use this theme

<style name="HomeTheme" parent="Theme.AppCompat.Light.NoActionBar">
     <item name="colorPrimary">@color/black</item>
    <item name="colorPrimaryDark">@color/black</item>
    <item name="android:windowBackground">@color/bg</item>
    <item name="android:textColorSecondary">@color/white</item>
    <item name="android:textColorPrimary">@color/white</item>
    <item name="colorAccent">@color/white</item>
    <item name="android:activatedBackgroundIndicator">@drawable/drawer_list_selector</item>
</style>

  and for toolbar, you can use
  <android.support.v7.widget.Toolbar
        android:id="@+id/main_toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:titleTextColor="@color/white"
        app:popupTheme="@style/AppTheme.PopupOverlay" >

  If still it not work, you can change toolbar color at programing time by
        toolbar.setTitleColor(getResources.getColor(R.color.white)

  Even still it not work, you can customize toolbar as

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


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/accept_order"
            android:layout_gravity="center"
            android:textSize="@dimen/twenty"
            android:textColor="@color/white"
            android:id="@+id/toolbar_title" />
      </android.support.v7.widget.Toolbar>