Action bar support not applying my style?
I was spending hours trying to fix the action bar title resizable according to this article: Android Toolbar: Small Title Text in Landscape Mode
Unfortunately, it seems that no matter what I do, everything about the styling of the action bar doesn't actually apply.
My v14 style files:
<!-- Main theme -->
<style name="MyTheme" parent="Theme.AppCompat">
<item name="android:actionBarItemBackground">@drawable/selectable_background</item>
<item name="android:popupMenuStyle">@style/PopupMenu</item>
<item name="android:dropDownListViewStyle">@style/DropDownListView.MyTheme</item>
<item name="android:actionBarTabStyle">@style/ActionBarTabStyle.MyTheme</item>
<item name="android:actionDropDownStyle">@style/DropDownNav.MyTheme</item>
<item name="android:spinnerItemStyle">@style/Widget.MyTheme.TextView.SpinnerItem</item>
<item name="android:spinnerDropDownItemStyle">@style/Widget.MyTheme.DropDownItem</item>
</style>
My ActionBar styles:
<style name="Widget.MyTheme.ActionBar" parent="@style/Widget.AppCompat.ActionBar.Solid">
<item name="android:background">@drawable/ab_solid</item>
<item name="android:titleTextStyle">@style/my_title_style</item>
</style>
<style name="my_title_style" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="textSize">20sp</item>
</style>
And of course "MyTheme" also applies in the manifest - I even tried to add it to all individual actions, but to no avail.
It seems that no matter what I do, things just won't apply. I tried to remove android: namespace, I tried to use titleTextAppearance instead of titleTextStyle, I basically emulated exactly how the parent themes do it, but won't change anything. Does anyone know what's going on? I recently updated AppCompat so I didn't quite understand how things work.
source to share
You need to use
<item name="background">@drawable/ab_solid</item>
<item name="titleTextStyle">@style/my_title_style</item>
to maintain library compatibility in your style.
Read Styling Action Bar , section " For Android 2.1 and higher
". Where it states that you need to use two records like
<style name="CustomActionBarTheme"
parent="@style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle">@style/MyActionBar</item>
<!-- Support library compatibility -->
<item name="actionBarStyle">@style/MyActionBar</item>
</style>
So your overall style will be
<style name="Widget.MyTheme.ActionBar" parent="@style/Widget.AppCompat.ActionBar.Solid">
<item name="android:background">@drawable/ab_solid</item>
<item name="android:titleTextStyle">@style/my_title_style</item>
<item name="background">@drawable/ab_solid</item>
<item name="titleTextStyle">@style/my_title_style</item>
</style>
source to share
Try this way:
-
Create a layout for
Toolbar
and supplyTextView
:<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_height="?attr/actionBarSize" android:layout_width="match_parent" android:id="@+id/toolbar" android:background="#b2b2b2"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:id="@+id/toolbar_title" android:textSize="20sp" android:textColor="#ff282828"/> </android.support.v7.widget.Toolbar>
-
Include
layout
in your.xml
:<include layout="@layout/toolbar_activities" android:id="@+id/toolbar_layout"/>
-
Finally, set the header to
java class
:Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_layout); TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title); mTitle.setText("Your Title"); setSupportActionBar(toolbar)
source to share