android – ActionBar customization

Question:

I want to customize the ActionBar: change the color of the text, the background. I try to do it through the styles, but it doesn't work. What's the catch? It works through the code, but not through xml.

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:actionBarStyle">@style/MyActionBar</item>
</style>
<style name="MyActionBar" parent="AppTheme">
    <item name="android:background">@color/colorAccent</item>
    <item name="android:textColor">@color/colorWhite</item>
</style>

Answer:

You can do this (via the ToolBar):

 <android.support.v7.widget.Toolbar     
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="?android:actionBarSize"
  >

  <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

      ...любые View

  </RelativeLayout>

</android.support.v7.widget.Toolbar>

accordingly, the ToolBar itself is customizable like any widget. Style can be added if needed:

xmlns:app="http://schemas.android.com/apk/res-auto"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"

in styles you need to turn off ActionBar:

<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>

in code:

ToolBar toolbar = (ToolBar) findViewById(R.id.tollbarId);
toolbar.inflateMenu(R.menu.menuId);
toolbar.setNavigationIcon(R.drawable.nav_con);

…etc.

Scroll to Top