Изменить цвет фона панели инструментов программно не меняет название панели инструментов Цвет фона
Я пытаюсь изменить панель инструментов Фоновый цвет программно, выполнив следующее:
getSupportActionBar().setBackgroundDrawable(newColorDrawable(getResources().getColor(R.color.test_color_blue)));
И это результат:
перед:
![enter image description here]()
После:
![enter image description here]()
Некоторые, как заголовок панели инструментов по-прежнему имеют тот же цвет фона, что и раньше.
вот моя панель инструментов xml:
<android.support.v7.widget.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="wrap_content"
android:gravity="center_vertical"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/Theme.Toolbar">
А вот тема:
<style name="Theme.Toolbar" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
<item name="android:maxHeight">@dimen/abc_action_bar_default_height_material</item>
<item name="android:background">@color/primary</item>
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/primary_dark</item>
<item name="colorAccent">@android:color/white</item>
<item name="titleTextAppearance">@style/Theme.Toolbar.Title</item>
</style>
Ответы
Ответ 1
Измените свой код следующим образом:
toolbar.xml
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/MyToolbarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">
Тема/Стиль
<style name="MyToolbarStyle">
<item name="android:maxHeight">@dimen/abc_action_bar_default_height_material</item>
<item name="android:background">@color/primary</item>
<item name="popupTheme">@style/ThemeOverlay.AppCompat.Light</item>
<item name="titleTextAppearance">@style/Theme.Toolbar.Title</item>
<!-- No need for colorPrimary, colorPrimaryDark, colorAccent here
this should go to the AppTheme -->
</style>
Результат
Перед установкой нового цвета фона:
![picture before background color change]()
и после:
![picture after background color change]()
Ответ 2
используйте это для доступа к текстовому окну
public void changeToggleTitle() {
if (mToolbar != null) {
for(int i= 0; i < mToolbar.getChildCount(); i++){
View v = mToolbar.getChildAt(i);
if(v != null && v instanceof TextView){
TextView t = (TextView) v;
// Do the magic
}
}
}
}
Ответ 3
Поздно, но я надеюсь, что будет полезным комментарий
У меня был этот item
в моем стиле активности
<item name="android:background">someColor</item>
поэтому, когда я изменил цвет toolbar
, заголовок и пункты меню не изменили фон.
Я просто удалил этот item
, и теперь он отлично работает.
У меня не было времени понять детали, но я думаю, что это может быть полезно кому-то другому.
Ответ 4
mToolbar.setBackgroundResource(mGameEnum.getPrimeColorRes());