android将工具栏标题颜色更改为白色

时间:2016-02-24 09:19:10

标签: android colors toolbar title

我的应用程序中的工具栏标题显示灰色而不是白色。下面是代码。请帮助我!!我编辑了代码。我已经包含了java代码和整个activity_main.xml

Java代码:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

activity_main.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:id="@+id/relative"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/bg7"
       >

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="#50000000"
            app:theme="@style/ActionBarThemeOverlay"
            app:popupTheme="@style/ActionBarPopupThemeOverlay"

            />

        <com.miguelcatalan.materialsearchview.MaterialSearchView
            android:id="@+id/search_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            style="@style/MaterialSearchView"/>

        <ImageView
            android:id="@+id/image2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:background="#90000000" />

        <RelativeLayout
            android:id="@+id/relative2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_centerVertical="true">

            <ImageView
                android:id="@+id/image1"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_centerVertical="true"
                android:layout_gravity="right|center_vertical"
                android:layout_marginLeft="20dp"
                android:background="@drawable/marker"></ImageView>

            <fr.ganfra.materialspinner.MaterialSpinner
                android:id="@+id/spinner1"

                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentRight="true"

                android:layout_marginLeft="50dp"
                android:layout_marginRight="10dp"
                app:ms_alignLabels="false"
                app:ms_arrowColor="@color/arrow"
                app:ms_arrowSize="8dp"
                app:ms_hintColor="@color/hint"
                app:ms_multiline="true"
                app:ms_thickness="0.0dp"
                app:popupTheme="@android:color/transparent"

                />


        </RelativeLayout>


    </RelativeLayout>

styles.xml:

 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>

</style>

colors.xml: &LT;

color name="colorPrimary">#8C9EFF</color>
    <color name="colorPrimaryDark">#903F51B5</color>
    <color name="colorAccent">#ffffff</color>

截图:enter image description here

7 个答案:

答案 0 :(得分:1)

将此添加到您的主题:

<item name="actionBarStyle">@style/Widget.ActionBar</item>

然后创建这种风格:

<style name="Widget.ActionBar" parent="@style/Widget.AppCompat.ActionBar">
    <item name="titleTextStyle">@style/TextAppearance.Widget.ActionBar.Title</item>
    <item name="subtitleTextStyle">@style/TextAppearance.Widget.ActionBar.Subtitle</item>
</style>

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

<style name="TextAppearance.Widget.ActionBar.Subtitle"
           parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Subtitle">
    <item name="android:textColor">@color/white</item>
</style>

答案 1 :(得分:0)

试试这个:

<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
style="@style/HeaderBar"
app:theme="@style/ActionBarThemeOverlay"
app:popupTheme="@style/ActionBarPopupThemeOverlay"/>

您可以使用以下样式完全控制您的ui元素:

<style name="ActionBarThemeOverlay" parent="">
<item name="android:textColorPrimary">#fff</item>
<item name="colorControlNormal">#fff</item>
<item name="colorControlHighlight">#3fff</item>
</style>

 <style name="HeaderBar">
<item name="android:background">?colorPrimary</item>
 </style>

<style name="ActionBarPopupThemeOverlay" parent="ThemeOverlay.AppCompat.Light" >
<item name="android:background">@android:color/white</item>
<item name="android:textColor">#000</item>
</style>

答案 2 :(得分:0)

将应用程序的主题设置为AndroidManifest.xml中的AppTheme

希望这有帮助。

<强>更新 将样式的parent更改为DarkActionBar并查看发生的情况。

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>

</style>

答案 3 :(得分:0)

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    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="50dp"
    android:background="#ffffff"
    app:theme="@style/ThemeOverlay.AppCompat.Light">

答案 4 :(得分:0)

您可以在应用栏内使用工具栏

<android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">


        <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/ThemeOverlay.AppCompat.Light"
            app:titleTextColor="#FFFFFF" />
    </android.support.design.widget.AppBarLayout>

或在你的style.xml文件中

<强> style.xml

<style name="AppTheme1" parent="Theme.AppCompat.Light">
        <item name="colorPrimary">@color/primary_color</item>
        <item name="android:actionBarStyle">@style/MyActionBar</item>
        <item name="android:popupMenuStyle">@style/popupMenuStyle</item>
    </style>

    <style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="android:background">@color/primary_color</item>
        <item name="android:icon">@drawable/ic_logo</item>

         <!--this is you want-->

        <item name="android:titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
    </style>

    <style name="MyTheme.ActionBar.TitleTextStyle" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
        <item name="android:textColor">#ffffff</item>
        <item name="android:textStyle">bold</item>
    </style>

    <style name="popupMenuStyle" parent="@android:style/Widget.PopupMenu">
        <item name="android:popupBackground">@color/new_popup_color</item>
    </style>

答案 5 :(得分:0)

工具栏中的

 style="@style/TitleTextWhite"

这在styles.xml

<style name="TitleTextWhite">
    <item name="android:textColor">@color/White</item>
</style>

答案 6 :(得分:0)

这是使用java

更改工具栏颜色的其他方法
toolbar.setTitleTextColor(ContextCompat.getColor(getApplicationContext(), R.color.YOUR_COLOR));

如果您的最低API级别低于23,则可以完成此任务。

与往常一样,如果我错了,请纠正我。