无法更改工具栏的颜色 [英] Can't change the color of toolbar
问题描述
任何人都可以请我解释一下,即使我创建了另一种样式,为什么我的工具栏也不会更改其颜色,
Can anyone please, explain me, why my toolbar doesn't change its color even though I created another style like:
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">false</item>
</style>
将其添加到清单文件中,例如:
added it in my manifest file like:
<activity
android:name=".SettingsActivity"
android:label="@string/title_activity_settings"
android:parentActivityName=".MainActivity">
<meta-data
android:theme="@style/AppTheme.NoActionBar"
android:name="android.support.PARENT_ACTIVITY"
android:value="com.com.com.com.MainActivity" />
</activity>
,然后尝试更改工具栏,例如:
and try to change the toolbar like:
Toolbar toolbar = new Toolbar(this);
toolbar.setBackgroundColor(getResources().getColor(R.color.light_blue));
setSupportActionBar(toolbar);
但仍然获得
此活动已经具有窗口装饰提供的操作栏. 不要请求Window.FEATURE_ACTION_BAR并将windowActionBar设置为 如果您将主题设置为false,则改为使用工具栏.
This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
我什至尝试getSupportActionBar().hide();
,但仍然无法正常工作.
I even tried to getSupportActionBar().hide();
but still doesn't work.
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.com.com.com.SettingsActivity"
android:id="@+id/settings_activity">
</RelativeLayout>
基本上,我想要保留我的全局主题,只是在其中一项活动中更改工具栏颜色.这就是为什么我不在xml文件中创建工具栏,而是以编程方式创建它,添加背景色并想要设置它的原因.
Basically, what I want is to keep my global theme and just change toolbar color in one of the activities. That is why I don't create a toolbar inside xml file, I create it programmatically, add background color and want to set it.
推荐答案
您需要在meta-data
标记之外起飞android:theme="@style/AppTheme.NoActionBar"
:
You need to take off android:theme="@style/AppTheme.NoActionBar"
outside the meta-data
tag :
<activity
android:name=".SettingsActivity"
android:label="@string/title_activity_settings"
android:theme="@style/AppTheme.NoActionBar"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="net.noorr.menote.menote.MainActivity" />
</activity>
在Java代码中:
Toolbar toolbar = (Toolbar) findViewById(R.id.yourToolbarId);
toolbar.setBackgroundColor(getResources().getColor(R.color.light_blue));
setSupportActionBar(toolbar);
这篇关于无法更改工具栏的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!