如何在Android中更改工具栏标题的颜色?

时间:2017-06-05 16:46:04

标签: android toolbar android-toolbar

我正在开发一个Android应用程序,我想让工具栏的标题为白色。

我删除了默认工具栏并为我的活动添加了一个工具栏,我无法弄清楚如何将工具栏标题的颜色更改为白色

MainActivity.java

package com.example.acer.videoapp;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;


public class MainActivity extends AppCompatActivity {

    Toolbar toolbar;
    ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle(getResources().getString(R.string.app_name));
        //toolbar.setTitleTextColor(white);
        listView = (ListView) findViewById(R.id.listView);


        ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_expandable_list_item_1,getResources().getStringArray(R.array.subjects));

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int i, long id) {
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                intent.putExtra("Subject", listView.getItemAtPosition(i).toString());
                startActivity(intent);
            }
        });
        listView.setAdapter(mAdapter);

    }
}

3 个答案:

答案 0 :(得分:2)

使用Toolbar方法

设置toolbar.setTitleTextColor()文字的颜色
 toolbar = (Toolbar) findViewById(R.id.toolbar);
 toolbar.setTitle(getResources().getString(R.string.app_name));
 toolbar.setTitleTextColor(getColor(R.color.white));

答案 1 :(得分:1)

修改styles.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
         <style name="MyTheme" parent="@android:style/Theme.Holo.Light">
            <item name="android:actionBarStyle">@style/MyTheme.ActionBarStyle</item>
         </style>

         <style name="MyTheme.ActionBarStyle" parent="@android:style/Widget.Holo.Light.ActionBar">
            <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">@color/white</item>
        </style>
    </resources>

然后将样式指定给工具栏

<android.support.v7.widget.Toolbar
     ...
     app:theme="@style/MyTheme">

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

答案 2 :(得分:0)

很简单,这对我有用(白色标题和图标):

 <android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="56dp"
    android:background="@color/PrimaryColor"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    android:elevation="4dp" />