如何更改 Android 中工具栏标题的颜色?
How to change color of toolbar title in 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);
}
}
修改你的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>
使用 toolbar.setTitleTextColor()
方法设置 Toolbar
文本的颜色
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle(getResources().getString(R.string.app_name));
toolbar.setTitleTextColor(getColor(R.color.white));
非常简单,这对我有用(白色标题和图标):
<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" />
我正在开发一个 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);
}
}
修改你的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>
使用 toolbar.setTitleTextColor()
方法设置 Toolbar
文本的颜色
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle(getResources().getString(R.string.app_name));
toolbar.setTitleTextColor(getColor(R.color.white));
非常简单,这对我有用(白色标题和图标):
<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" />