I've added a Toolbar to my Lollipop project. However, I'm having trouble styling the Toolbar. My title in the Toolbar appears to have a bold font, but I'd like it to be italic. Any idea how to achieve this? I've tried playing around with the actionbar-style, no luck.
I've set a style on the Toolbar with app:theme="@style/toolbar" and my @style/toolbar (parent ThemeOverlay.AppCompat.ActionBar) is where I'm playing around with no good results.
You can add TextView inside Toolbar, for example :
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary" >
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="italic" />
This is actually really easy. There's a method on Toolbar called setTitleTextAppearance() that everyone seems to be overlooking. Just define your custom textAppearance in styles.xml, such as:
@android:color/white
20sp
and then in your code, you just call :
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitleTextAppearance(this, R.style.MyTitleTextApperance);
and voila!
The ToolBar title is stylable. Any customization you make has to be made in the theme. I'll give you an example.
Toolbar layout:
style="@style/ToolBarStyle.Event"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="@dimen/abc_action_bar_default_height_material" />
Styles:
@style/ThemeOverlay.AppCompat.Light
@style/ThemeOverlay.AppCompat.Dark.ActionBar
@style/TextAppearance.Widget.Event.Toolbar.Title
normal
@dimen/event_title_text_size
Dynameyes
The problem mentioned above only occurs in Lollipop versions and not in Kitkat and lower versions. Lollipop does not follow the text style and put its own TitleTextAppearance. So even you add your own TextView child for your Toolbar (shown below), the android.support.v7.widget.Toolbar will still override your styles.
android:id="@+id/toolbar"
android:theme="@style/someToolbarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:id="@+id/title"
android:textAppearance="@style/TextAppearance.AppCompat.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
To finally solve this, put your own TextView for the title and override the setTitle methods of the Toolbar to skip the Lollipop's predefined TitleTextAppearance.
public class YourCustomToolbar extends Toolbar {
public YourCustomToolbar (Context context) {
super(context);
}
public YourCustomToolbar (Context context, AttributeSet attrs) {
super(context, attrs);
}
public YourCustomToolbar (Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void setTitle(CharSequence title) {
((TextView) findViewById(R.id.title)).setText(title);
}
@Override
public void setTitle(int title) {
((TextView) findViewById(R.id.title)).setText(title);
}
}
You can apply a theme to the toolbar as follows:
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="56dp"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
Just modify the style in your styles.xml, changing the TextAppearance attribute, and you should be good to go.
来源:https://stackoverflow.com/questions/26577528/android-toolbar-style