Change action bar color in android

前端 未结 9 1773
北海茫月
北海茫月 2020-12-05 19:28

I am using the app theme Theme.Black in my app. In this theme the action bar is gray. How I can change color of my action bar? This is my attempt:



        
相关标签:
9条回答
  • 2020-12-05 20:09

    if you have in your layout activity_main

    <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay" />
    

    you have to put

    in your Activity this code

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        toolbar.setBackgroundColor(Color.CYAN);
    }
    
    0 讨论(0)
  • 2020-12-05 20:14

    onclick buttons c2 to fetch and change the color of action bar and notification bar according to button's background, hope this helps

    int color = c2.getBackground()).getColor();
    int colorlighter = -color * 40 / 100 + color;
    getWindow().setNavigationBarColor(colorlighter);
    getWindow().setStatusBarColor(colorlighter);
    ActionBar bar = getSupportActionBar();
    bar.setBackgroundDrawable(new ColorDrawable(color));
    

    colorlighter is used to set color of notification bar a little lighter than action bar
    color is the background color of button c2, according to which we change our color.

    0 讨论(0)
  • 2020-12-05 20:17

    Updated code:

    getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.your_color)));
    
    0 讨论(0)
提交回复
热议问题