简体   繁体   中英

Java android change color of actionbar

Simply want to change the color of my actionbar!

So, when I google this, it appears pretty simple...

I've seen people propose this several times:

<style name="AppTheme"
    parent="@style/Theme.AppCompat.Light.DarkActionBar">
    <item name="android:actionBarStyle">@style/MyActionBar</item>
</style>

<!-- ActionBar styles -->
<style name="MyActionBar"
    parent="@style/Widget.AppCompat.Light.ActionBar">
    <item name="android:background">#990000</item>
</style>

But I really cant get this to work. It doesn't do any thing different.. Actionbar is still black. But exactly this seems to work for other people, so I dont understand..

When I try to put this:

    ActionBar actionBar;

    actionBar = getActionBar();
    ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#990000"));
    actionBar.setBackgroundDrawable(colorDrawable);

in my onCreate, the app just crashes...

Edit: Okay I'm an idiot for posting way too soon... Just had to put the actionBar code below the:

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

It works now, sorry for posting before I tried that...

The new Lollipop AppCompat library does not use android: namespace for theming. It is highly probable that you theming will work if you remove android: and do it like below:

<style name="AppTheme"
    parent="@style/Theme.AppCompat.Light.DarkActionBar">
    <item name="actionBarStyle">@style/MyActionBar</item>
</style>

The better way of doing is to use Toolbar instead of deprecated ActionBar . To do it like that, your theme should parent from NoActionBar theme.

<style name="AppTheme"
    parent="@style/Theme.AppCompat.NoActionBar">
</style>

And you have to include Toolbar into your own layouts like below. That way you can set whatever background you want.

<android.support.v7.widget.Toolbar
  android:id="@+id/toolbar_actionbar"
  android:layout_width="match_parent"
  android:layout_height="?attr/actionBarSize"
  android:background="#990000" />

将主题应用于清单上的整个应用程序或单个活动:

<application android:theme="@style/CustomActionBarTheme" ... />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM