首先,我强烈建议您迁移到新方法 - Toolbar。它更加灵活,您可以将其自定义为普通的View。
关于您的问题。
您可以通过编程方式获取 ActionBar 对象和 setBackground。
这是一个简短的例子
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable("COLOR IN HEX 0xFFFF6666 for instance"));
我将展示我将如何实现它。这更多是关于架构和模式。
为Fragment 使用一些基类,最好也为Activity 使用基类。让我们考虑
public class BaseFragment extends Fragment
你的片段所在的 Activity 类。
public class MainActivity extends Activity
在这种情况下你必须定义Activity的职责并创建接口
在你的情况下与ActionBar合作
创建界面
public interface ActionBarProvider {
void setActionBarColor(ColorDrawable color);
}
让你的activity实现这个接口
public class MainActivity extends Activity implements ActionBarProvider {
public void setActionBarColo(ColorDrawable color) {
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(color));
}
}
最后在BaseFragmentonAttach
public void onAttach(Context context) {
super.onAttach(context);
mActionBarProvider = (ActionBarProvider) context;
}
使mActionBarProvider 变量受保护并使每个片段扩展BaseFragment,您可以从任何片段中更改操作栏颜色mActionBarProvider.setActionBarColor(new ColorDrawable());
希望这会有所帮助。