2017-06-05 117 views
0

我正在開發一個android應用程序,我想讓工具欄的標題爲白色。如何更改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); 

    } 
} 
+0

toolbar.setTitleTextColor(白色)是不工作的Toolbar文本的顏色? – sasikumar

+0

沒有工作,我不知道爲什麼 – Dilinieee

回答

2

設置使用toolbar.setTitleTextColor()方法

toolbar = (Toolbar) findViewById(R.id.toolbar); 
toolbar.setTitle(getResources().getString(R.string.app_name)); 
toolbar.setTitleTextColor(getColor(R.color.white)); 
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> 
相關問題