我在一个片段中,我想在用户点击按钮时更改操作栏颜色并查看背景颜色。无论用户选择何种颜色,我目前在下面的代码都会导致操作栏变为灰色。
private void changeColor( int colorId ) {
ActionBar actionBar = ((ActionBarActivity )getActivity()).getSupportActionBar();
actionBar.setBackgroundDrawable( new ColorDrawable( colorId ) );
this.getView().setBackgroundColor( getResources().getColor( colorId ) );
}
我的styles.xml中的动作栏主题为蓝色,不确定是否有任何效果。如果您有任何建议,请告诉我。
谢谢, 森
解决方案: 不得不改变:
actionBar.setBackgroundDrawable( new ColorDrawable( colorId ) );
要:
actionBar.setBackgroundDrawable( new ColorDrawable( getResources().getColor( colorId ) ) );
答案 0 :(得分:8)
如果我正确理解您的问题,您想要根据用户干预更改颜色基础,请尝试相反。
public void setActionBarColor(int parsedColor){
ActionBar mActionBar = getSupportActionBar();
mActionBar.setBackgroundDrawable(new ColorDrawable(parsedColor));
mActionBar.setDisplayShowTitleEnabled(false);
mActionBar.setDisplayShowTitleEnabled(true);
}
在片段中使用它
// check if instance of activity is MyActivity just an example
MyActivity mA = ((MyActivity)getActivity());
mA.setActionBarColor(Color.parseColor("#FFA2C13E"));
希望它有所帮助:)
PS:我正在使用ActionBarActivity