如何使用不同的视图更改工具栏颜色? [英] How to change Toolbar Color with different views?
问题描述
我正在主要活动中设置工具栏,并尝试使用不同的片段更改背景颜色. 所以基本上,我试图访问片段内的工具栏对象并设置不同的背景颜色. 我尝试做的几件事是:
I am setting up my Toolbar in my main activity and trying to change the background color with different fragments. So basically, I am trying to access the Toolbar object inside fragment and set different background color. Few things which I have tried to do is :
访问工具栏,例如: (((ActionBarActivity)getActivity()).getSupportActionBar().setBackgroundColor(XXX);
Access toolbar like: ((ActionBarActivity)getActivity()).getSupportActionBar().setBackgroundColor(XXX);
但是我无法访问片段中的setBackgroundColor函数.在Main Activity中可以正常工作.
But I am unable to access the setBackgroundColor function inside fragment. It is perfectly working inside the Main Activity.
推荐答案
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable("COLOR"));
或
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0000ff")));
看看这
这篇关于如何使用不同的视图更改工具栏颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!