【问题标题】:Change Background Color of AppCompat Action Bar更改 AppCompat 操作栏的背景颜色
【发布时间】:2014-11-22 02:54:31
【问题描述】:

我在一个片段中,我想在用户单击按钮时更改操作栏颜色并查看背景颜色。无论用户选择什么颜色,我目前在下面的代码都会导致操作栏变灰。

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 ) ) );

【问题讨论】:

  • 如果从 styles.xml 中删除蓝色会怎样?
  • 同样的问题,我还注意到,当我尝试在我的列表视图行之一上设置线性布局的背景颜色时,我得到相同的灰色。 holder.titleLayout.setBackgroundColor(item.getColorCode());
  • @Nath5,请不要在 cmets 中回答问题。

标签: android colors android-actionbar android-appcompat


【解决方案1】:

如果我正确理解您的问题,即您想根据用户干预更改颜色,请改用此方法。

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

【讨论】:

    猜你喜欢
    • 2014-06-03
    • 1970-01-01
    • 1970-01-01
    • 2015-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多