我想在我正在尝试的操作栏中更改标题颜色,如下所示:
<style name="MyTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <item name="actionBarStyle">@style/MyTheme.ActionBarStyle</item> <item name="android:actionBarStyle">@style/MyTheme.ActionBarStyle</item> <item name="android:titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item> <item name="titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item> <item name="drawerArrowStyle">@style/AppTheme.DrawerArrowToggle</item> <item name="colorAccent">@color/greenText</item> </style> <style name="MyTheme.ActionBarStyle" parent="@android:style/Widget.Holo.Light.ActionBar"> <item name="android:titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item> <item name="background">@color/whiteText</item> </style> <style name="AppTheme.DrawerArrowToggle" parent="Base.Widget.AppCompat.DrawerArrowToggle"> <item name="color">@android:color/black</item> </style> <style name="MyTheme.ActionBar.TitleTextStyle" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title"> <item name="android:textColor">@color/actionbar_blackText</item> </style>
在我的清单中:我使用相同的主题.
在我的Activity类中:我将FragmentActivity更改为AppCompatActivity
OnCreate看起来像这样:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getSupportActionBar().setDisplayOptions(getSupportActionBar().getDisplayOptions() | android.support.v7.app.ActionBar.DISPLAY_SHOW_CUSTOM); ImageView imageView = new ImageView(getSupportActionBar().getThemedContext()); imageView.setScaleType(ImageView.ScaleType.CENTER); imageView.setImageResource(R.drawable.logoforactionbar); android.support.v7.app.ActionBar.LayoutParams layoutParams = new android.support.v7.app.ActionBar.LayoutParams(android.support.v7.app.ActionBar.LayoutParams.WRAP_CONTENT,android.support.v7.app.ActionBar.LayoutParams.WRAP_CONTENT,Gravity.END | Gravity.CENTER_VERTICAL); layoutParams.rightMargin = 20; imageView.setLayoutParams(layoutParams); getSupportActionBar().setCustomView(imageView); getSupportActionBar().setTitle(getResources().getString(R.string.register_new)); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setHomeButtonEnabled(true); getSupportActionBar().setIcon(android.R.color.transparent); setContentView(R.layout.register_new); }
但标题文字颜色不会改变,或者我也看不到黑色向上箭头.
文字和箭头是白色的……
奇怪的是,在具有导航抽屉的另一个AppCompatActivity上也能正常工作.
我得到这样的结果:
我剪了徽标部分.无论如何,应该有黑色标题和黑色箭头,但不会出现.
解决方法
您只需要粘贴它并使用您自己的XML更改此XML中的颜色
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <item name="colorPrimaryDark">#cc0986ff</item> <item name="colorPrimary">#ff0c84e8</item> <item name="android:textColorPrimary">#fdfff2</item> <item name="android:windowBackground">@android:color/white</item> <item name="android:editTextColor">@android:color/background_dark</item> <item name="android:textColor">@android:color/background_dark</item> <item name="colorControlNormal">@android:color/background_dark</item> <item name="colorControlActivated">#ff0043c5</item> <item name="colorControlHighlight">#ffcb6345</item> </style>