问题
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 :
Access toolbar like: ((ActionBarActivity)getActivity()).getSupportActionBar().setBackgroundColor(XXX);
But I am unable to access the setBackgroundColor function inside fragment. It is perfectly working inside the Main Activity.
回答1:
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable("COLOR"));
or
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0000ff")));
Have a look This or This
回答2:
It is very easy to change the ToolBar, Actionbar color.
ActionBar bar = getSupportActionBar();
bar.setBackgroundDrawable(new ColorDrawable("COLOR"));
or
ActionBar bar = getSupportActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0000ff")));
来源:https://stackoverflow.com/questions/29298454/how-to-change-toolbar-color-with-different-views