Custom (gradient) background of ActionBar Compat

后端 未结 4 1585
遥遥无期
遥遥无期 2021-02-05 13:59

I am using Action Bar Compat so that my action bar with navigation drawer was backward compatible down to API level 9 and I want to change the background of the action bar.

4条回答
  •  温柔的废话
    2021-02-05 14:50

    Another approach, without modifying styles.xml:

    Here is our example GradientDrawable in res/drawable/ab_gradient.xml

    
    
    
        
    
    
    

    You can set it to action bar in your Activity's onCreate():

    ActionBar actionBar = getActionBar();    
    actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.action_bar_gradient_shape));
    

    If you use support v7 library (your case):

    // make sure to import android.support.v7.app.ActionBar
    ActionBar actionBar = getSupportActionBar();        
    actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.action_bar_gradient_shape));
    

    Or if you use ActionBarSherlock:

    // make sure to import com.actionbarsherlock.app.ActionBar
    ActionBar actionBar = getSherlock().getActionBar();     
    actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.action_bar_gradient_shape));
    

提交回复
热议问题